Beispiel #1
0
        protected override void OnTranslate(ShaderTranslationContext sc, MethodDeclarationSyntax syntax, ScopeInfo scope)
        {
            MethodInfo info = sc.GetMethodInfo(syntax);

            if (info != null)
            {
                MappedEntryPoint ep = null;
                sc.EntryPointsByMethod.TryGetValue(info, out ep);

                sc.Source.AppendLineBreak();
                ScopeInfo mScope = sc.Source.OpenScope(ScopeType.Method, null, ep);
                mScope.Method = info;

                ShaderType returnType = ShaderType.TranslateType(sc, syntax.ReturnType.ToString());

                ep?.Translator?.TranslatePrefix(sc, info, syntax, ep);
                sc.Source.Append($"{returnType.Translation} {syntax.Identifier.ValueText}");

                sc.Complete(syntax.ReturnType);
                sc.Complete(syntax.ConstraintClauses);
                sc.Complete(syntax.AttributeLists);

                if (syntax.TypeParameterList != null)
                {
                    sc.CompleteSelfAndChildren(syntax.TypeParameterList);
                }

                // Translate parameters before method body.
                sc.Runner.Translate(sc, syntax.ParameterList, 0);
                ep?.Translator?.TranslatePostfix(sc, info, syntax, ep);
            }
        }
        protected override void OnTranslate(ShaderTranslationContext sc, PredefinedTypeSyntax syntax, ScopeInfo scope)
        {
            string     typeName = syntax.Keyword.ToString();
            ShaderType type     = ShaderType.TranslateType(sc, typeName);

            sc.Source.Append(type.Translation);
        }
        protected override void OnTranslate(ShaderTranslationContext sc, PropertyDeclarationSyntax syntax, ScopeInfo scope)
        {
            ScopeInfo classScope = scope.FindOfType(ScopeType.Class);
            ScopeInfo pScope     = sc.Source.OpenScope(ScopeType.Property);

            pScope.Identifier = syntax.Identifier.ValueText;

            sc.Complete(syntax.Type);

            pScope.TypeInfo = ShaderType.TranslateType(sc, syntax.Type.ToString());
        }
        protected override void OnTranslate(ShaderTranslationContext sc, ObjectCreationExpressionSyntax syntax, ScopeInfo scope)
        {
            // Are we directly inside an initializer? (i.e. array initializer)
            if (scope.Type == ScopeType.ArrayInitializer)
            {
                if (syntax != scope.Items.Last())
                {
                    sc.Source.OpenScope(ScopeType.ArrayElement);
                }
            }

            string     typeName = syntax.Type.ToString();
            ShaderType type     = ShaderType.TranslateType(sc, typeName);

            sc.Complete(syntax.Type);

            // Handle initializers. These require declaring as a local variable above the current syntax node.
            if (syntax.Initializer != null)
            {
                switch (syntax.Parent)
                {
                case ReturnStatementSyntax returnSyntax:
                case AssignmentExpressionSyntax assignSyntax:
                    string strInit = type.Translation;
                    string varName = sc.Parent.GetNewVariableName("oc_init");

                    scope.DeclareLocal(sc, () =>
                    {
                        sc.Source.Append($"{type.Translation} {varName} = {type.Translation}");
                        sc.Runner.Translate(sc, syntax.ArgumentList);
                        sc.Source.Append(";");
                        sc.Source.AppendLineBreak();

                        ScopeInfo iScope  = sc.Source.OpenScope(ScopeType.ExpandedInitializer);
                        iScope.Identifier = varName;

                        sc.Runner.Translate(sc, syntax.Initializer);
                    });

                    sc.Source.Append(varName);
                    break;

                default:
                    sc.Source.Append(type.Translation);
                    break;
                }
            }
            else
            {
                sc.Source.Append(type.Translation);
            }
        }
        protected override void OnTranslate(ShaderTranslationContext sc, VariableDeclarationSyntax syntax, ScopeInfo scope)
        {
            string     typeName = syntax.Type.ToString();
            ShaderType type     = ShaderType.TranslateType(sc, typeName);

            ScopeInfo tScope = sc.Source.OpenScope(ScopeType.Typed, type);

            tScope.TypeInfo = type;
            tScope.IsLocal  = syntax.Parent is LocalDeclarationStatementSyntax;
            tScope.Items    = syntax.Variables;

            if (syntax.Parent is FieldDeclarationSyntax fieldSyntax)
            {
                tScope.TranslatedModifiers = sc.Language.TranslateModifiers(fieldSyntax.Modifiers);
            }

            sc.Complete(syntax.Type);
        }
Beispiel #6
0
        protected override void OnTranslate(ShaderTranslationContext sc, ParameterSyntax syntax, ScopeInfo scope)
        {
            ScopeInfo        methodScope = scope.FindOfType(ScopeType.Method);
            MethodInfo       methodInfo  = null;
            MappedEntryPoint ep          = null;

            if (scope.Type == ScopeType.Parentheses)
            {
                if (scope.Items.Last() != syntax)
                {
                    sc.Source.OpenScope(ScopeType.ParenthesesItem);
                }
            }

            if (methodScope != null)
            {
                methodInfo = methodScope.Method;
                sc.EntryPointsByMethod.TryGetValue(methodInfo, out ep);
            }

            // TODO pass to language variable translation, since parameters can have attributes
            ShaderType type        = ShaderType.TranslateType(sc, syntax.Type.ToString());
            bool       wasAppended = false;

            if (ep != null)
            {
                (ParameterInfo pInfo, int pIndex) = sc.GetParameterInfo(methodInfo, syntax.Identifier.ValueText);
                IEnumerable <Attribute> pAttributes = pInfo.GetCustomAttributes();

                ep.Translator?.TranslateParameterPrefix(sc, syntax, ep, pInfo, pAttributes, pIndex);
                sc.Source.Append($"{type.Translation} {syntax.Identifier.ValueText}");
                ep.Translator?.TranslateParameterPostfix(sc, syntax, ep, pInfo, pAttributes, pIndex);
                wasAppended = true;
            }

            if (!wasAppended)
            {
                sc.Source.Append($"{type.Translation} {syntax.Identifier.ValueText}");
            }

            sc.Complete(syntax.Type);
            sc.Complete(syntax.AttributeLists);
        }