private void GenerateConstructor(CodeGeneratorProxy codeGenerator, MixinLevelCodeGeneratorPipelineState manager) { var constructorBodyStatements = new StringBuilder(); #region Mixin Instance Initialization Statement constructorBodyStatements.AppendFormat( "{0} = {1}", MasterWrapperPlan.MixinInstanceDataMemberName, manager.MixinGenerationPlan.MasterWrapperPlan.MixinInstanceInitializationStatement .EnsureEndsWith(";")); #endregion #region Initialize Virtual Member Functions var masterWrapperMemberHelper = new MasterWrapperCodeGeneratorProxyMemberHelper( codeGenerator, manager.CommonState.Context.TypeResolver.Compilation); manager.MixinGenerationPlan.MasterWrapperPlan.VirtualMembers. Map(mw => constructorBodyStatements.Append( GenerateVirtualMemberFunctionInitializationStatement(mw, masterWrapperMemberHelper))); #endregion #region Initialize BaseMasterWrapper Statement constructorBodyStatements.AppendFormat( "base.Initialize( {0}, {1}, new global::System.Collections.Generic.List<global::{2}>{{ {3} }});", MasterWrapperPlan.TargetInstanceConstructorParameterName, MasterWrapperPlan.MixinInstanceDataMemberName, typeof(IMixinInterceptor).GetOriginalFullName(), string.Join(",", manager.MixinGenerationPlan.MixinAttribute.Interceptors .Select <IType, string>(x => x.GenerateActivationExpression()) ) ); #endregion codeGenerator.CreateConstructor( modifiers: "public", parameters: new[] { new KeyValuePair <string, string>( manager.MixinGenerationPlan.ExternalMixinSpecificAutoGeneratedNamespaceName.EnsureEndsWith( ".") + manager.MixinGenerationPlan.RequirementsInterfacePlan.RequirementsInterfaceName, MasterWrapperPlan.TargetInstanceConstructorParameterName) }, constructorInitializer: string.Empty, constructorBody: constructorBodyStatements.ToString()); }
private void ProcessVirtualMembers( MasterWrapperCodeGeneratorProxyMemberHelper proxyMemberHelper, MixinLevelCodeGeneratorPipelineState manager) { var virtualMethods = manager.MixinGenerationPlan.MasterWrapperPlan.VirtualMembers .Where(mw => mw.Member is IMethod); var virtualPropertiesAndFields = manager.MixinGenerationPlan.MasterWrapperPlan.VirtualMembers .Where(mw => mw.Member is IProperty || mw.Member is IField); #region Methods proxyMemberHelper.CreateMembers( virtualMethods, generateMemberModifier: member => "public", baseObjectIdentifierFunc: member => "this", baseObjectMemberNameFunc: member => member.ImplementationDetails.VirtualMemberFunctionName ); #endregion #region Properties //virtual properties need to be created directly. foreach (var virtualProp in virtualPropertiesAndFields) { proxyMemberHelper.CodeGeneratorProxy.CreateProperty( modifier: "internal", returnTypeFullName: virtualProp.Member.ReturnType.GetOriginalFullNameWithGlobal(), propertyName: virtualProp.Member.Name, getterMethodBody: (!(virtualProp.Member is IProperty) || !(virtualProp.Member as IProperty).CanGet) ? string.Empty : string.Format( "get{{ return base.ExecutePropertyGet(\"{0}\", () => {1}Get();}}", virtualProp.Member.Name, virtualProp.ImplementationDetails.VirtualMemberFunctionName), setterMethodBody: (!(virtualProp.Member is IProperty) || !(virtualProp.Member as IProperty).CanSet) ? string.Empty : string.Format( "set{{ base.ExecutePropertySet(\"{0}\", value, (v) => {1}Set(v);}}", virtualProp.Member.Name, virtualProp.ImplementationDetails.VirtualMemberFunctionName) ); } #endregion }
private void ProcessRegularMembers( MasterWrapperCodeGeneratorProxyMemberHelper proxyMemberHelper, MixinLevelCodeGeneratorPipelineState manager) { proxyMemberHelper.CreateMembers( manager.MixinGenerationPlan.MasterWrapperPlan.RegularMembers, generateMemberModifier: member => "public", baseObjectIdentifierFunc: member => MasterWrapperPlan.MixinInstanceDataMemberName); }
private void ProcessProtectedAbstractMembers( MasterWrapperCodeGeneratorProxyMemberHelper proxyMemberHelper, MixinLevelCodeGeneratorPipelineState manager) { proxyMemberHelper.CreateMembers( manager.MixinGenerationPlan.MasterWrapperPlan.ProtectedAbstractMembers, generateMemberModifier: member => "public", baseObjectIdentifierFunc: member => MasterWrapperPlan.MixinInstanceDataMemberName, baseObjectMemberNameFunc: member => member.ImplementationDetails.ProtectedAbstractMemberPromotedToPublicMemberName ); }
private void ProcessStaticMembers( MasterWrapperCodeGeneratorProxyMemberHelper proxyMemberHelper, MixinLevelCodeGeneratorPipelineState manager) { proxyMemberHelper.CreateMembers( manager.MixinGenerationPlan.MasterWrapperPlan.StaticMembers, generateMemberModifier: member => "public static", baseObjectIdentifierFunc: member => manager.MixinGenerationPlan.AbstractWrapperPlan.GenrateAbstractWrapper ? manager.AbstractMembersWrapper.GetFullTypeName() : manager.MixinGenerationPlan.MixinAttribute.Mixin.GetOriginalFullNameWithGlobal() ); }
public bool PerformTask(MixinLevelCodeGeneratorPipelineState manager) { var proxyMemberHelper = new MasterWrapperCodeGeneratorProxyMemberHelper( new CodeGeneratorProxy(manager.MasterWrapper), manager.CommonState.Context.TypeResolver.Compilation); ProcessStaticMembers(proxyMemberHelper, manager); ProcessProtectedAbstractMembers(proxyMemberHelper, manager); ProcessVirtualMembers(proxyMemberHelper, manager); ProcessRegularMembers(proxyMemberHelper, manager); ProcessExplicitlyImplementedMembers(proxyMemberHelper, manager); return(true); }
private void ProcessExplicitlyImplementedMembers( MasterWrapperCodeGeneratorProxyMemberHelper proxyMemberHelper, MixinLevelCodeGeneratorPipelineState manager) { proxyMemberHelper.CreateMembers( manager.MixinGenerationPlan.MasterWrapperPlan.ImplementExplicitlyMembers, generateMemberModifier: member => "", generateMemberNameFunc: member => string.Format("{0}.{1}", member.ImplementationDetails.ExplicitInterfaceImplementationType .GetOriginalFullNameWithGlobal(), (member.Member is IProperty) && (member.Member as IProperty).IsIndexer ? string.Format("this [{0} {1}]", (member.Member as IProperty).Parameters.First().Type.GetOriginalFullNameWithGlobal(), (member.Member as IProperty).Parameters.First().Name) : member.Member.Name), baseObjectIdentifierFunc: member => string.Format("( ({0}) {1} )", member.ImplementationDetails.ExplicitInterfaceImplementationType .GetOriginalFullNameWithGlobal(), MasterWrapperPlan.MixinInstanceDataMemberName)); //Implement Explicit interfaces manager.MixinGenerationPlan.MasterWrapperPlan.ImplementExplicitlyMembers .Select(x => x.ImplementationDetails.ExplicitInterfaceImplementationType .GetOriginalFullNameWithGlobal()) .Map(t => proxyMemberHelper.CodeGeneratorProxy.ImplementInterface(t)); }
/// <summary> /// Wrap every non-private member in current pMixin with special /// handling for static and virtual members /// </summary> /// <param name="manager"></param> /// <param name="wrapperClass"></param> private void ProcessMembers(pMixinGeneratorPipelineState manager, ICodeGeneratorProxy wrapperClass) { var proxyMemberHelper = new MasterWrapperCodeGeneratorProxyMemberHelper(wrapperClass, manager.BaseState.Context.TypeResolver.Compilation); //Static Members proxyMemberHelper.CreateMembers( manager.CurrentMixinMembers .Select(x => x.Member) .DistinctMembers() .Where(member => member.IsStatic), generateMemberModifier: member => "internal static", baseObjectIdentifierFunc: member => manager.CurrentpMixinAttribute.Mixin.IsStaticOrSealed() ? manager.CurrentpMixinAttribute.Mixin.GetOriginalFullNameWithGlobal() : manager.CurrentMixinAbstractMembersWrapperClass.Name ); //Regular Members proxyMemberHelper.CreateMembers( manager.CurrentMixinMembers .Select(x => x.Member) .DistinctMembers() .Where( member => !member.IsStatic && !(member.IsAbstract && member.IsProtected) && !member.IsOverride && !member.IsOverridable && !member.IsVirtual), generateMemberModifier: member => "internal", baseObjectIdentifierFunc: member => MixinInstanceDataMemberName); //Protected Abstract Members proxyMemberHelper.CreateMembers( manager.CurrentMixinMembers .Select(x => x.Member) .DistinctMembers() .Where(member => (member.IsAbstract || member.IsOverride) && member.IsProtected), generateMemberModifier: member => "internal", baseObjectIdentifierFunc: member => MixinInstanceDataMemberName, baseObjectMemberNameFunc: member => GenerateAbstractMixinMembersWrapperClass.GetProtectedMemberWrapperMemberName(member)); //Virtual Members foreach ( var member in manager.CurrentMixinMembers .Select(x => x.Member) .DistinctMembers() .Where(member => member.IsVirtual || (member.IsOverride || member.IsOverridable && !member.IsProtected))) { #region Virtual Method if (member is IMethod) { //Virtual methods call to the Virtual Method Func wrapperClass.CreateMethod( "internal", member.ReturnType.GetOriginalFullNameWithGlobal(), member.Name, (member as IMethod).Parameters.ToKeyValuePair(), string.Format("{0} {1}({2});", (member as IMethod).GetReturnString(), GenerateVirtualMethodFuncName(member), string.Join(",", (member as IMethod).Parameters.Select(x => x.Name))), (member as IMethod).GetGenericMethodConstraints( manager.BaseState.Context.TypeResolver.Compilation)); } #endregion #region Virtual Property else if (member is IProperty) { wrapperClass.CreateProperty( "internal", member.ReturnType.GetOriginalFullNameWithGlobal(), member.Name, (member as IProperty).CanGet && !(member as IProperty).Getter.IsPrivate ? string.Format("get{{ return {0}(); }}", GenerateVirtualMethodFuncName(member) + "Get") : "", (member as IProperty).CanSet && !(member as IProperty).Setter.IsPrivate ? string.Format("set {{{0}(value); }}", GenerateVirtualMethodFuncName(member) + "Set") : "" ); } #endregion } }
private string CreateConstructorInitializersForVirtualMemberFunctions( pMixinGeneratorPipelineState manager, ICodeGeneratorProxy wrapperClass) { var proxyMemberHelper = new MasterWrapperCodeGeneratorProxyMemberHelper(wrapperClass, manager.BaseState.Context.TypeResolver.Compilation); var sb = new StringBuilder(); foreach ( var member in manager.CurrentMixinMembers .Select(x => x.Member) .DistinctMembers() .Where(member => !member.IsPrivate && (member.IsVirtual || member.IsOverride || member.IsOverridable))) { if (member is IMethod) { //ProtectedVirtualMethodFunc = (i) => AbstractWrapper.ProtectedVirtualMethod(i); sb.AppendFormat("{0} = ({1}) => {2};", GenerateVirtualMethodFuncName(member), string.Join(",", (member as IMethod).Parameters.Select(x => x.Name)), proxyMemberHelper.GetMethodBodyCallStatement( member as IMethod, (m) => MixinInstanceDataMemberName, (m) => m.IsProtected ? GenerateAbstractMixinMembersWrapperClass.GetProtectedMemberWrapperMemberName(m) : m.Name)); } else if (member is IProperty) { if ((member as IProperty).CanGet) { //PublicVirtaulPropertyFuncGet = () => AbstractWrapper.PublicVirtualProperty; sb.AppendFormat( @"{0}Get = () => {1};", GenerateVirtualMethodFuncName(member), proxyMemberHelper.GetPropertyGetterReturnBodyStatement( member as IProperty, (m) => MixinInstanceDataMemberName, (m) => m.IsProtected ? GenerateAbstractMixinMembersWrapperClass.GetProtectedMemberWrapperMemberName(m) : m.Name)); } if ((member as IProperty).CanSet) { //PublicVirtualPropertySetFunc = (s) => AbstractWrapper.PublicVirtualProperty = s; sb.AppendFormat( @"{0}Set = (value) => {1};", GenerateVirtualMethodFuncName(member), proxyMemberHelper.GetPropertySetterReturnBodyStatement( member as IProperty, (m) => MixinInstanceDataMemberName, (m) => m.IsProtected ? GenerateAbstractMixinMembersWrapperClass.GetProtectedMemberWrapperMemberName(m) : m.Name)); } } } return(sb.ToString()); }
private string GenerateVirtualMemberFunctionInitializationStatement( MemberWrapper mw, MasterWrapperCodeGeneratorProxyMemberHelper masterWrapperMemberHelper) { #region mw.Member is IMethod if (mw.Member is IMethod) { //ProtectedVirtualMethodFunc = (i) => AbstractWrapper.ProtectedVirtualMethod(i); return(string.Format("{0} = ({1}) => {2}.{3}({1});", mw.ImplementationDetails.VirtualMemberFunctionName, string.Join(",", (mw.Member as IMethod).Parameters.Select(x => x.Name)), MasterWrapperPlan.MixinInstanceDataMemberName, mw.Member.Name)); } #endregion #region mw.Member is IProperty if (mw.Member is IProperty) { return(string.Format( "{0}" + //Get "{1}", //Set (!(mw.Member as IProperty).CanGet) ? string.Empty : #region Get string.Format( "{0}Get = () => {1}.{2};", mw.ImplementationDetails.VirtualMemberFunctionName, MasterWrapperPlan.MixinInstanceDataMemberName, mw.Member.Name), #endregion (!(mw.Member as IProperty).CanSet) ? string.Empty : #region Set string.Format( "{0}Set = (value) => {1}.{2} = value;", mw.ImplementationDetails.VirtualMemberFunctionName, MasterWrapperPlan.MixinInstanceDataMemberName, mw.Member.Name) #endregion )); } #endregion return(string.Empty); }