Esempio n. 1
0
        public static MethodDeclarationSyntax CreateMethod(MethodGenerationData data)
        {
            Debug.Assert(!string.IsNullOrEmpty(data.m_MethodName), "Trying to generate a method with null or empty method name!");
            Debug.Assert(!string.IsNullOrEmpty(data.m_MethodReturnType), "Trying to generate a method with null or empty return type!");
            Debug.Assert(data.m_MethodBodyStatements != null && data.m_MethodBodyStatements.Count > 0, "Trying to generate a method with no body!");

            MethodDeclarationSyntax syntax = SyntaxFactory
                                             .MethodDeclaration(SyntaxFactory.ParseTypeName(data.m_MethodReturnType), data.m_MethodName)
                                             .AddModifiers(CodeGenerationUtility.CreateProtectionLevelToken(data.m_ProtectionLevel));

            switch (data.m_InheritanceKeyword)
            {
            case FunctionInheritanceKeyword.STATIC: syntax = syntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword)); break;

            case FunctionInheritanceKeyword.OVERRIDE: syntax = syntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.OverrideKeyword)); break;

            case FunctionInheritanceKeyword.VIRTUAL: syntax = syntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.VirtualKeyword)); break;
            }

            if (data.m_IsAsync)
            {
                bool canMakeAsync = false;

                for (int i = 0; i < s_AcceptedAsyncReturnTypes.Length; i++)
                {
                    if (!data.m_MethodReturnType.Contains(s_AcceptedAsyncReturnTypes[i]))
                    {
                        continue;
                    }

                    canMakeAsync = true;
                    break;
                }

                Debug.Assert(canMakeAsync, "Trying to generate async function but the return type is not supported! Please make the return type either void, Task or UniTask");

                if (canMakeAsync)
                {
                    syntax = syntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.AsyncKeyword));
                }
            }

            syntax = syntax.AddAttributeLists(AttributeGenerationService.CreateAttributeListSyntaxes(data.m_Attributes));

            foreach (var param in data.m_MethodParams)
            {
                syntax = syntax.AddParameterListParameters(CodeGenerationUtility.CreateParameterSyntax(param.m_ParamName, param.m_ParamType));
            }

            foreach (var statement in data.m_MethodBodyStatements)
            {
                syntax = syntax.AddBodyStatements((SyntaxFactory.ParseStatement(statement)));
            }

            return(syntax);
        }
Esempio n. 2
0
        /// <summary> Creates the fields in the generated class. Such as "private int testInt = 0" </summary>
        public static FieldDeclarationSyntax[] CreateFieldDeclarationSyntaxes(List <FieldGenerationData> datas)
        {
            var syntaxes = new List <FieldDeclarationSyntax>();

            for (int i = 0; i < datas.Count; i++)
            {
                // Variable Type: bool, Variable Name: Cancelled. Created variable => bool Cancelled
                VariableDeclarationSyntax variableSyntax = SyntaxFactory
                                                           .VariableDeclaration(SyntaxFactory.ParseTypeName(datas[i].m_VariableType))
                                                           .AddVariables(CreateVariableDeclaratorSyntax(datas[i]));

                // With Protection Level = bool Cancelled => private bool Cancelled
                syntaxes.Add(SyntaxFactory.FieldDeclaration(variableSyntax)
                             .AddModifiers(CodeGenerationUtility.CreateProtectionLevelToken(datas[i].m_ProtectionLevel))
                             .AddModifiers(CreateStaticFieldModifierTokens(datas[i])));
            }

            return(syntaxes.ToArray());
        }