static void AddErrorHelpers(CodeTypeDeclaration type)
        {
            var minusOne = Expression.Primitive(-1);

            var errors = type.AddProperty <CompilerErrorCollection> ("Errors").AsProtected()
                         .WithGetLazyInitialize(
                type.AddField <CompilerErrorCollection> ("errors"),
                init: Expression.New <CompilerErrorCollection> ())
                         .OnThis();

            type.AddMethod("Error")
            .WithParameter <string> ("message", out var paramErrorMessage)
            .WithStatements(
                errors.InvokeMethod(
                    "Add",
                    Expression.New <CompilerError> (Expression.Null, minusOne, minusOne, Expression.Null, paramErrorMessage)
                    ));

            type.AddMethod("Warning")
            .WithParameter("message", TypeReference.String, out var paramWarningMessage)
            .WithStatements(
                Statement.DeclareVariable <CompilerError> ("val",
                                                           Expression.New <CompilerError> (Expression.Null, minusOne, minusOne, Expression.Null, paramWarningMessage),
                                                           out var val),
                val.SetProperty("IsWarning", Expression.True),
                errors.InvokeMethod("Add", val).AsStatement()
                );
        }
 private static void AddProperties(this CodeTypeDeclaration targetClass, CodeNamespace codeNamespace, IRecordDescriptor descriptor, out bool hasArrayType)
 {
     hasArrayType = false;
     foreach (var field in descriptor.Fields)
     {
         if (field.Value.IsArray)
         {
             codeNamespace.GenerateClass((IRecordDescriptor)field.Value, $"{field.Key}Item", out hasArrayType);
             targetClass.AddArrayProperty(field.Key, $"{field.Key}Item");
             hasArrayType = true;
             continue;
         }
         targetClass.AddProperty(field.Key, field.Value);
     }
 }
        static void GenerateProcessingHelpers(CodeTypeDeclaration type)
        {
            var builderFieldDef = type.AddField <StringBuilder> ("builder").WithReference(out var builder);
            var sessionFieldDef = type.AddField <IDictionary <string, object> > ("session");

            type.AddPropertyGetSet("Session", sessionFieldDef).AsVirtual();

            type.AddProperty <StringBuilder> ("GenerationEnvironment")
            .WithSet(builder)
            .WithGetLazyInitialize(builder, builderFieldDef.Type.New());

            AddErrorHelpers(type);
            AddIndentHelpers(type);
            AddWriteHelpers(type);
        }
Example #4
0
        CodeTypeDeclaration CreateMySettingsProperty(SettingsDocument setDoc)
        {
            CodeTypeDeclaration c = new CodeTypeDeclaration("MySettingsProperty");

            c.UserData["Module"] = true;
            c.AddAttribute(new CodeTypeReference("Microsoft.VisualBasic.HideModuleNameAttribute"));
            c.AddAttribute(typeof(DebuggerNonUserCodeAttribute));
            c.AddAttribute(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute));
            c.TypeAttributes = TypeAttributes.NotPublic;

            CodeTypeReference r = new CodeTypeReference(setDoc.GeneratedFullClassName);
            var p = c.AddProperty(r, "Settings");

            p.Attributes = MemberAttributes.Assembly | MemberAttributes.Static;
            p.Getter.Return(Easy.Type(r).Property("Default"));
            p.AddAttribute(typeof(System.ComponentModel.Design.HelpKeywordAttribute), Easy.Prim("My.Settings"));
            return(c);
        }
Example #5
0
        private void Create_SOContainer(CodeNamespace pNameSpace, CodeNamespaceImport[] arrDefaultUsing, CodeTypeDeclaration pType, CodeTypeDeclaration[] arrEnumType, out CodeTypeDeclaration pContainerType, out CodeMemberMethod pInitMethod)
        {
            string strContainerTypeName = pType.Name + "_Container";

            pContainerType = new CodeTypeDeclaration(strContainerTypeName);
            pContainerType.AddBaseClass("UnityEngine.ScriptableObject");

            pNameSpace.Imports.Clear();
            pNameSpace.Imports.AddRange(arrDefaultUsing);
            pNameSpace.Imports.Add(new CodeNamespaceImport("System.Linq"));
            pNameSpace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            pNameSpace.Types.Clear();
            pNameSpace.Types.Add(pContainerType);
            pNameSpace.Types.AddRange(arrEnumType);

            pContainerType.AddField(new FieldTypeData(const_strFieldName_private_instance, strContainerTypeName), MemberAttributes.Private | MemberAttributes.Static);
            var pPublicInstanceProperty = pContainerType.AddProperty(new FieldTypeData(const_strFieldName_instance, strContainerTypeName), MemberAttributes.Public | MemberAttributes.Static);

            pPublicInstanceProperty.GetStatements.Add(new CodeSnippetStatement($"               return {const_strFieldName_private_instance};"));

            pContainerType.AddField(new FieldTypeData(const_strFieldName_ListData, $"List<{pType.Name}>"));
            pInitMethod = Generate_InitMethod(pContainerType, pType.Name);
        }
Example #6
0
 static void ProcessType(Type type, CodeTypeDeclaration ctd)
 {
     foreach (FieldInfo field in type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic))
     {
         ctd.AddField(ConvertType(field.FieldType), field.Name).Attributes = 0;
     }
     foreach (FieldInfo field in type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic))
     {
         EasyProperty p = ctd.AddProperty(ConvertType(field.FieldType), GetPropertyName(field.Name));
         p.Getter.Return(Easy.Var(field.Name));
         CodeExpression ex;
         if (field.FieldType.IsValueType)
         {
             ex = new CodePropertySetValueReferenceExpression();
         }
         else
         {
             ex = GetDefaultValue("value", field);
         }
         p.Setter.Assign(Easy.Var(field.Name), ex);
         if (typeof(INode).IsAssignableFrom(field.FieldType))
         {
             if (typeof(INullable).IsAssignableFrom(field.FieldType))
             {
                 p.SetStatements.Add(new CodeSnippetStatement("\t\t\t\tif (!" + field.Name + ".IsNull) " + field.Name + ".Parent = this;"));
             }
             else
             {
                 p.SetStatements.Add(new CodeSnippetStatement("\t\t\t\t" + field.Name + ".Parent = this;"));
             }
         }
     }
     foreach (ConstructorInfo ctor in type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
     {
         CodeConstructor c = new CodeConstructor();
         if (type.IsAbstract)
         {
             c.Attributes = MemberAttributes.Family;
         }
         else
         {
             c.Attributes = MemberAttributes.Public;
         }
         ctd.Members.Add(c);
         ConstructorInfo baseCtor = GetBaseCtor(type);
         foreach (ParameterInfo param in ctor.GetParameters())
         {
             c.Parameters.Add(new CodeParameterDeclarationExpression(ConvertType(param.ParameterType),
                                                                     param.Name));
             if (baseCtor != null && Array.Exists(baseCtor.GetParameters(), delegate(ParameterInfo p) { return(param.Name == p.Name); }))
             {
                 continue;
             }
             c.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression(GetPropertyName(param.Name)),
                                                      new CodeVariableReferenceExpression(param.Name)));
         }
         if (baseCtor != null)
         {
             foreach (ParameterInfo param in baseCtor.GetParameters())
             {
                 c.BaseConstructorArgs.Add(new CodeVariableReferenceExpression(param.Name));
             }
         }
         // initialize fields that were not initialized by parameter
         foreach (FieldInfo field in type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic))
         {
             if (field.FieldType.IsValueType && field.FieldType != typeof(Location))
             {
                 continue;
             }
             if (Array.Exists(ctor.GetParameters(), delegate(ParameterInfo p) { return(field.Name == p.Name); }))
             {
                 continue;
             }
             c.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression(field.Name),
                                                      GetDefaultValue(null, field)));
         }
     }
 }
Example #7
0
 public static CodeMemberProperty AddPropertyGetOnly(this CodeTypeDeclaration type, string propertyName, CodeMemberField backingField)
 => type.AddProperty(propertyName, backingField.Type)
 .WithGet(Expression.This.Field(backingField));
Example #8
0
 public static CodeMemberProperty AddProperty <T> (this CodeTypeDeclaration type, string propertyName)
 => type.AddProperty(propertyName, TypeReference <T> .Global);
Example #9
0
        public virtual CodeTypeDeclaration CreateClass(SettingsDocument setDoc)
        {
            CodeTypeDeclaration c = new CodeTypeDeclaration(setDoc.GeneratedClassName);

            c.AddAttribute(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute));
            c.AddAttribute(typeof(System.CodeDom.Compiler.GeneratedCodeAttribute),
                           Easy.Prim(typeof(SettingsCodeGeneratorTool).FullName),
                           Easy.Prim(typeof(SettingsCodeGeneratorTool).Assembly.GetName().Version.ToString()));
            c.TypeAttributes = TypeAttributes.NotPublic | TypeAttributes.Sealed;
            c.IsPartial      = true;
            c.BaseTypes.Add(Easy.TypeRef(typeof(ApplicationSettingsBase)));

            CodeMemberField f = c.AddField(Easy.TypeRef(c), "defaultInstance");

            f.Attributes     = MemberAttributes.Private | MemberAttributes.Static;
            f.InitExpression = Easy.Type(typeof(ApplicationSettingsBase))
                               .InvokeMethod("Synchronized", Easy.New(Easy.TypeRef(c)))
                               .CastTo(Easy.TypeRef(c));

            var defaultProperty = c.AddProperty(f, "Default");

            if (setDoc.UseMySettingsClassName)
            {
                c.AddAttribute(typeof(EditorBrowsableAttribute), Easy.Prim(EditorBrowsableState.Advanced));
                AddAutoSaveLogic(c, defaultProperty);
            }

            foreach (SettingsEntry entry in setDoc.Entries)
            {
                Type           entryType      = entry.Type ?? typeof(string);
                SpecialSetting?specialSetting = null;
                foreach (SpecialTypeDescriptor desc in SpecialTypeDescriptor.Descriptors)
                {
                    if (desc.type == entryType)
                    {
                        entryType      = typeof(string);
                        specialSetting = desc.specialSetting;
                        break;
                    }
                }
                EasyProperty p = c.AddProperty(entryType, entry.Name);
                if (entry.Scope == SettingScope.User)
                {
                    p.AddAttribute(typeof(UserScopedSettingAttribute));
                }
                else
                {
                    p.AddAttribute(typeof(ApplicationScopedSettingAttribute));
                }
                if (!string.IsNullOrEmpty(entry.Provider))
                {
                    p.AddAttribute(typeof(SettingsProviderAttribute),
                                   Easy.TypeOf(new CodeTypeReference(entry.Provider)));
                }
                if (!string.IsNullOrEmpty(entry.Description))
                {
                    p.AddAttribute(typeof(SettingsDescriptionAttribute), Easy.Prim(entry.Description));
                    Easy.AddSummary(p, entry.Description);
                }
                p.AddAttribute(typeof(DebuggerNonUserCodeAttribute));
                if (specialSetting != null)
                {
                    p.AddAttribute(typeof(SpecialSettingAttribute),
                                   Easy.Prim(specialSetting.Value));
                }
                if (entry.GenerateDefaultValueInCode)
                {
                    p.AddAttribute(typeof(DefaultSettingValueAttribute), Easy.Prim(entry.SerializedValue));
                }
                if (entry.Scope == SettingScope.User && entry.Roaming)
                {
                    p.AddAttribute(typeof(SettingsManageabilityAttribute),
                                   Easy.Prim(SettingsManageability.Roaming));
                }
                p.Getter.Return(Easy.This.Index(Easy.Prim(entry.Name)).CastTo(entryType));
//				p.GetStatements.Add(new CodeMethodReturnStatement(
//					new CodeCastExpression(new CodeTypeReference(entryType),
//					                       new CodeIndexerExpression(new CodeThisReferenceExpression(),
//					                                                 new CodePrimitiveExpression(entry.Name))
//					                      )
//				));
                if (entry.Scope == SettingScope.User)
                {
                    p.Setter.Assign(Easy.This.Index(Easy.Prim(entry.Name)), Easy.Value);
                }
            }

            return(c);
        }
Example #10
0
        private void GenerateClient(string name, CodeNamespace ns, int complex_type_count)
        {
            CodeTypeDeclaration type = ns.AddType(name);

            type.IsClass        = true;
            type.TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed;
            type.BaseTypes.Add(typeof(RpcAlpcClientBase));

            CodeConstructor constructor = type.AddConstructor(MemberAttributes.Public | MemberAttributes.Final);

            constructor.BaseConstructorArgs.Add(CodeGenUtils.GetPrimitive(_server.InterfaceId.ToString()));
            constructor.BaseConstructorArgs.Add(CodeGenUtils.GetPrimitive(_server.InterfaceVersion.Major));
            constructor.BaseConstructorArgs.Add(CodeGenUtils.GetPrimitive(_server.InterfaceVersion.Minor));

            foreach (var proc in _server.Procedures)
            {
                string proc_name = proc.Name;
                if (!_proc_names.Add(proc_name))
                {
                    proc_name = $"{proc_name}_{proc.ProcNum}";
                    if (!_proc_names.Add(proc_name))
                    {
                        throw new ArgumentException($"Duplicate name {proc.Name}");
                    }
                }

                var method = type.AddMethod(proc_name, MemberAttributes.Public | MemberAttributes.Final);
                RpcTypeDescriptor return_type = GetTypeDescriptor(proc.ReturnValue.Type);
                if (return_type == null)
                {
                    method.ThrowNotImplemented("Return type unsupported.");
                    continue;
                }

                var offset_to_name =
                    proc.Params.Select(p => Tuple.Create(p.Offset, p.Name)).ToList();

                method.ReturnType = return_type.CodeType;
                method.CreateMarshalObject(MARSHAL_NAME);
                foreach (var p in proc.Params)
                {
                    if (p == proc.Handle)
                    {
                        continue;
                    }
                    RpcTypeDescriptor p_type = GetTypeDescriptor(p.Type);

                    List <RpcMarshalArgument> extra_marshal_args = new List <RpcMarshalArgument>();
                    if (p_type.VarianceDescriptor.IsValid)
                    {
                        extra_marshal_args.Add(p_type.VarianceDescriptor.CalculateCorrelationArgument(p.Offset, offset_to_name));
                    }

                    var p_obj = method.AddParam(p_type.GetParameterType(), p.Name);
                    p_obj.Direction = p.GetDirection();
                    if (!p.IsIn)
                    {
                        continue;
                    }
                    if (p_type.Pointer)
                    {
                        if (p_type.PointerType == RpcPointerType.Reference)
                        {
                            method.AddNullCheck(MARSHAL_NAME, p.Name);
                        }
                        else
                        {
                            method.AddWriteReferent(MARSHAL_NAME, p.Name);
                        }
                    }
                    else if (!p_type.ValueType)
                    {
                        method.AddNullCheck(MARSHAL_NAME, p.Name);
                    }
                    method.AddMarshalCall(p_type, MARSHAL_NAME, p.Name, extra_marshal_args.ToArray());
                    // If it's a constructed type then ensure any deferred writes are flushed.
                    if (p_type.Constructed)
                    {
                        method.AddFlushDeferredWrites(MARSHAL_NAME);
                    }
                }

                method.SendReceive(MARSHAL_NAME, UNMARSHAL_NAME, proc.ProcNum);

                foreach (var p in proc.Params.Where(x => x.IsOut))
                {
                    if (p == proc.Handle)
                    {
                        continue;
                    }

                    RpcTypeDescriptor p_type = GetTypeDescriptor(p.Type);
                    if (p_type.Pointer)
                    {
                        method.AddPointerUnmarshalCall(p_type, UNMARSHAL_NAME, p.Name);
                    }
                    else
                    {
                        method.AddUnmarshalCall(p_type, UNMARSHAL_NAME, p.Name);
                    }
                    if (p_type.Constructed)
                    {
                        method.AddPopluateDeferredPointers(UNMARSHAL_NAME);
                    }
                }

                method.AddUnmarshalReturn(return_type, UNMARSHAL_NAME);
            }

            if (complex_type_count > 0 && HasFlag(RpcClientBuilderFlags.GenerateConstructorProperties))
            {
                var constructor_type = new CodeTypeReference(CodeGenUtils.MakeIdentifier(CONSTRUCTOR_STRUCT_NAME));
                var prop             = type.AddProperty("New", constructor_type, MemberAttributes.Public | MemberAttributes.Final,
                                                        new CodeMethodReturnStatement(new CodeObjectCreateExpression(constructor_type)));
                constructor_type = new CodeTypeReference(CodeGenUtils.MakeIdentifier(ARRAY_CONSTRUCTOR_STRUCT_NAME));
                type.AddProperty("NewArray", constructor_type, MemberAttributes.Public | MemberAttributes.Final,
                                 new CodeMethodReturnStatement(new CodeObjectCreateExpression(constructor_type)));
            }
        }