Ejemplo n.º 1
0
        public string sig()
        {
            string sig = access.ToString().ToLower() + " ";

            if (type == MethodType.StaticFunction || type == MethodType.Extension)
            {
                sig += "static ";
            }
            if (type == MethodType.Abstract)
            {
                sig += "abstract ";
            }
            else if (type == MethodType.Override)
            {
                sig += "override ";
            }
            else if (type == MethodType.Virtual)
            {
                sig += "virtual ";
            }

            if (async)
            {
                sig += "async ";
            }

            sig += $"{(returnType != null ? returnType.RealName(true) : "")} {name}{(genericSuffix.Valid() ? $"{genericSuffix}" : "")}";

            var funcFirstArg = type == MethodType.Extension ? $"this {classType.RealName(true)} self" : "";

            sig += $"({CodeGenTools.MergeSig(funcFirstArg, args)}) {constraints}";

            return(sig);
        }
        static void GenerateConstructionFromRoot(Type type)
        {
            var rootType = type.FindTagInHierarchy <RootType>()?.type;

            if (rootType == null)
            {
                return;
            }

            Action <MethodBuilder> fillCreateWithLivableSetup = sink =>
            {
                sink.content($"inst.root = this;");
//                if (type.HasReferenceId())
//                    sink.content($"inst.Id = entityIdFactory++;");
                //if (type.HasNestedLivableChildren())
                sink.content($"inst.{SetupHierarchyFuncName}();");
                sink.content($"inst.{SetupChildrenIdFuncName}();");
                //if (type.HasChildrenThatNeedsRootSetup())
            };

            var constructorMethodFlags = BindingFlags.DeclaredOnly | BindingFlags.Instance
                                         | BindingFlags.Public | BindingFlags.NonPublic;

            if (type.IsAbstract == false && (!type.IsGenericType || type.IsConstructedGenericType))
            {
                bool hasConstructor = false;
                foreach (var methodInfo in type.GetMethods(constructorMethodFlags))
                {
                    if (methodInfo.Name.StartsWith("Ctor"))
                    {
                        var sig = methodInfo.GetParameters().Select(p => $"{p.ParameterType.RealName(true)} {p.Name}")
                                  .PrintCollection();
                        var call = methodInfo.GetParameters().Select(p => p.Name).PrintCollection();
                        // ctor mwthod found
                        var constructFull = GenClassSink(rootType).Method(type.CreateLivableInRootFunc(), rootType, MethodType.Instance, type, sig, "", "");
                        constructFull.indent++;
                        //constructFull.content($"var inst = pool.{type.GetFromPoolFunc()}();");
                        CreateNewInstance(constructFull, new DataInfo {
                            type = type, baseAccess = "inst", sureIsNull = true
                        }, "", true, "", true);
                        fillCreateWithLivableSetup(constructFull);

                        constructFull.content($"inst.{methodInfo.Name}({call});");
                        constructFull.content($"return inst;");
                        hasConstructor = true;
                    }
                }

                if (!hasConstructor)
                {
                    var createWithSetup = GenClassSink(rootType).Method(type.CreateLivableInRootFunc(), rootType, MethodType.Instance, type, "", "", "");
                    createWithSetup.indent++;
                    CreateNewInstance(createWithSetup, new DataInfo {
                        type = type, baseAccess = "inst", sureIsNull = true
                    }, "", true, "", true);
                    fillCreateWithLivableSetup(createWithSetup);
                    createWithSetup.content($"return inst;");
                }
            }

            // Create from prototype
            if ((type.ReadGenFlags() & GenTaskFlags.UpdateFrom) != 0)
            {
                var createFromProrotype = GenClassSink(rootType).Method(type.CreateLivableInRootFunc(), rootType, MethodType.Instance, type, $"{type.RealName(true)} prototype", "", "");
                createFromProrotype.indent++;
                //CreateNewInstance(createFromProrotype, new DataInfo{type = type, baseAccess = "inst", sureIsNull = true}, "", true, "", true );
                //createFromProrotype.content($"var inst = ({type.RealName(true)})prototype.NewInst();");
                GenUpdateValueFromInstance(createFromProrotype, new DataInfo {
                    type = type, baseAccess = $"inst", sureIsNull = true
                }, "prototype", false, needCreateVar: true);
                fillCreateWithLivableSetup(createFromProrotype);
                createFromProrotype.content($"return inst;");
            }

            if (polymorphicRootNodes.ContainsKey(type))
            {
                MethodInfo polymorphicConstructor = null;
                foreach (var methodInfo in type.GetMethods(constructorMethodFlags))
                {
                    if (methodInfo.Name.StartsWith("Prepare") && (methodInfo.IsVirtual || methodInfo.IsAbstract))
                    {
                        var sig = methodInfo.GetParameters().Select(p => $"{p.ParameterType.RealName(true)} {p.Name}")
                                  .PrintCollection();
                        var enumTypeRef = type.PolymorphicRootTypeEnumName();
                        if (string.IsNullOrEmpty(type.Namespace) == false)
                        {
                            enumTypeRef = type.Namespace + "." + enumTypeRef;
                        }
                        sig = CodeGenTools.MergeSig($"{enumTypeRef} classId", sig);
                        var call = methodInfo.GetParameters().Select(p => p.Name).PrintCollection();
                        // ctor mwthod found
                        var constructFull = GenClassSink(rootType).Method($"CreatePolymorphic{type.UniqueName(false)}",
                                                                          rootType, MethodType.Instance, type, sig, "", "");
                        constructFull.indent++;
                        constructFull.content($"var inst = {type.NewPolymorphicFromClassIdExpression(type.NeedsPooledPolymorphConstruction())};");
                        fillCreateWithLivableSetup(constructFull);
                        constructFull.content($"inst.{methodInfo.Name}({call});");
                        constructFull.content($"return inst;");
                    }
                }
            }
        }