예제 #1
0
        public override ExpressionSyntax GetCachedValue(string key, ExpressionSyntax value, TypeHandle modelReturnType, params IdentifierNameSyntax[] attributes)
        {
            key = key.Replace(".", "_");
            if (m_Constants.TryGetValue(key, out var node))
            {
                return(node);
            }

            string variableName = MakeUniqueName(key);

            var fieldDeclarationSyntax = RoslynBuilder.DeclareField(
                modelReturnType.Resolve(IterationContext.Stencil),
                variableName, AccessibilityFlags.Public);

            if (attributes.Any())
            {
                fieldDeclarationSyntax = fieldDeclarationSyntax.AddAttributeLists(AttributeList(SeparatedList(attributes.Select(Attribute))));
            }
            m_MemberDeclarations.Add(fieldDeclarationSyntax);

            IdentifierNameSyntax variable = IdentifierName(variableName);

            m_JobInitializers.Add(AssignmentExpression(
                                      SyntaxKind.SimpleAssignmentExpression,
                                      variable,
                                      value
                                      ));

            m_Constants.Add(key, variable);

            return(variable);
        }
예제 #2
0
        IEnumerable <MemberDeclarationSyntax> BuildComponentMembers(RoslynTranslator translator)
        {
            var states     = new List <SwitchSectionSyntax>();
            var variables  = new Dictionary <string, FieldDeclarationSyntax>();
            var stateIndex = 0;

            // Add members (MoveNext + variables) from coroutine nodes in stack
            var stack = Parent.IterationContext.Query;

            BuildStack(translator, stack, ref variables, ref states, ref stateIndex);

            // Fields
            var members = new List <MemberDeclarationSyntax>
            {
                RoslynBuilder.DeclareField(
                    typeof(int),
                    RoslynEcsBuilder.CoroutineStateVariableName,
                    AccessibilityFlags.Private)
            };

            members.AddRange(variables.Values);

            // Parameters + Arguments
            foreach (var component in m_AccessedComponents)
            {
                var componentName = GetComponentVariableName(IterationContext.Query, component);
                var componentType = component.Resolve(IterationContext.Stencil);

                m_Parameters.Add(
                    Argument(IdentifierName(componentName))
                    .WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)),
                    Parameter(Identifier(componentName))
                    .WithModifiers(TokenList(Token(SyntaxKind.RefKeyword)))
                    .WithType(TypeSystem.BuildTypeSyntax(componentType)));
            }

            foreach (var localVariable in IterationContext.Query.FunctionVariableModels)
            {
                m_Parameters.Add(
                    Argument(IdentifierName(localVariable.Name)),
                    Parameter(Identifier(localVariable.Name))
                    .WithType(TypeSystem.BuildTypeSyntax(localVariable.DataType.Resolve(IterationContext.Stencil))));
            }

            // Create MoveNext method
            members.Add(
                RoslynBuilder.DeclareMethod(k_MoveNext, AccessibilityFlags.Public, typeof(bool))
                .WithParameterList(
                    ParameterList(SeparatedList(m_Parameters.Values)))
                .WithBody(
                    Block(
                        SwitchStatement(IdentifierName(RoslynEcsBuilder.CoroutineStateVariableName))
                        .WithOpenParenToken(Token(SyntaxKind.OpenParenToken))
                        .WithCloseParenToken(Token(SyntaxKind.CloseParenToken))
                        .WithSections(List(states)),
                        ReturnStatement(
                            LiteralExpression(SyntaxKind.FalseLiteralExpression)))));

            return(members);
        }
예제 #3
0
        public override ExpressionSyntax GetOrDeclareCommandBuffer(bool isConcurrent)
        {
            if (m_CommandBuffers.TryGetValue(isConcurrent, out var syntaxName))
            {
                return(syntaxName);
            }

            var commandBufferName = isConcurrent ? "ConcurrentCommandBuffer" : "CommandBuffer";
            var commentBufferType = isConcurrent ? typeof(EntityCommandBuffer.Concurrent) : typeof(EntityCommandBuffer);

            syntaxName = IdentifierName(commandBufferName);
            m_CommandBuffers.Add(isConcurrent, syntaxName);

            var field = RoslynBuilder.DeclareField(
                commentBufferType,
                commandBufferName,
                AccessibilityFlags.Public);

            m_MemberDeclarations.Add(field);

            var assignment = AssignmentExpression(
                SyntaxKind.SimpleAssignmentExpression,
                IdentifierName(commandBufferName),
                Parent.GetOrDeclareCommandBuffer(isConcurrent));

            m_JobInitializers.Add(assignment);

            return(syntaxName);
        }
예제 #4
0
        public override string GetOrDeclareComponentArray(RoslynEcsTranslator.IterationContext ctx, ComponentDefinition componentDefinition, out LocalDeclarationStatementSyntax arrayInitialization, out StatementSyntax arrayDisposal)
        {
            arrayInitialization = null;
            arrayDisposal       = null;

            if (m_DeclaredComponentArray.TryGetValue(componentDefinition, out var arrayName))
            {
                return(arrayName);
            }

            Type componentType = componentDefinition.TypeHandle.Resolve(ctx.Stencil);
            Type arrayType     = typeof(NativeArray <>).MakeGenericType(componentType);

            arrayName = ctx.GetComponentDataArrayName(componentType);
            m_DeclaredComponentArray.Add(componentDefinition, arrayName);

            FieldDeclarationSyntax field = RoslynBuilder.DeclareField(arrayType, arrayName, AccessibilityFlags.Public);

            m_MemberDeclarations.Add(AddAttribute(field, nameof(DeallocateOnJobCompletionAttribute)));

            m_JobInitializers.Add(AssignmentExpression(
                                      SyntaxKind.SimpleAssignmentExpression,
                                      IdentifierName(arrayName),
                                      RootContext.MakeInitComponentDataArrayExpression(ctx, componentType)
                                      ));

            return(ctx.GetComponentDataArrayName(componentType));
        }
예제 #5
0
 protected override StatementSyntax GetOrDeclareEntityArray(RoslynEcsTranslator.IterationContext iterationContext, out StatementSyntax arrayDisposal)
 {
     m_MemberDeclarations.Insert(0, AddAttribute(
                                     RoslynBuilder.DeclareField(
                                         typeof(NativeArray <Entity>),
                                         iterationContext.EntitiesArrayName,
                                         AccessibilityFlags.Public),
                                     nameof(DeallocateOnJobCompletionAttribute),
                                     nameof(NativeDisableParallelForRestrictionAttribute)));
     m_JobInitializers.Add(AssignmentExpression(
                               SyntaxKind.SimpleAssignmentExpression,
                               IdentifierName(iterationContext.EntitiesArrayName),
                               ForEachContext.MakeInitEntityArray(iterationContext)));
     arrayDisposal = null;
     return(null); // nothing to declare in context
 }
        protected IEnumerable <MemberDeclarationSyntax> BuildComponentMembers()
        {
            // Fields
            var members = new List <MemberDeclarationSyntax>
            {
                RoslynBuilder.DeclareField(
                    typeof(int),
                    k_CoroutineStateVariableName,
                    AccessibilityFlags.Public)
            };

            members.AddRange(m_ComponentVariables.Values);

            foreach (var component in m_AccessedComponents)
            {
                var componentName = GetComponentVariableName(IterationContext.Query, component);
                var componentType = component.Resolve(IterationContext.Stencil);

                m_Parameters.Add(
                    Argument(IdentifierName(componentName))
                    .WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)),
                    Parameter(Identifier(componentName))
                    .WithModifiers(TokenList(Token(SyntaxKind.RefKeyword)))
                    .WithType(TypeSystem.BuildTypeSyntax(componentType)));
            }

            foreach (var localVariable in IterationContext.Query.FunctionVariableModels)
            {
                m_Parameters.Add(
                    Argument(IdentifierName(localVariable.Name)),
                    Parameter(Identifier(localVariable.Name))
                    .WithType(TypeSystem.BuildTypeSyntax(localVariable.DataType.Resolve(IterationContext.Stencil))));
            }

            if ((TranslationOptions & RoslynEcsTranslator.TranslationOptions.Tracing) != 0)
            {
                m_Parameters.Add(
                    Argument(GetRecorderName())
                    .WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)),
                    Parameter(GetRecorderName().Identifier)
                    .WithModifiers(SyntaxTokenList.Create(Token(SyntaxKind.RefKeyword)))
                    .WithType((IsJobContext ? typeof(GraphStream.Writer) : typeof(DebuggerTracer.EntityFrameTrace))
                              .ToTypeSyntax()));
            }

            return(members);
        }
        void DeclareAndInitField(Type t, string name, AccessibilityFlags accessibility = AccessibilityFlags.Private,
                                 ExpressionSyntax initValue = null)
        {
            var entityGroup = RoslynBuilder
                              .DeclareField(t, name)
                              .WithModifiers(TokenList(RoslynBuilder.AccessibilityToSyntaxToken(accessibility)));

            AddMember(entityGroup);

            if (initValue == null)
            {
                return;
            }

            StatementSyntax init = ExpressionStatement(
                AssignmentExpression(
                    SyntaxKind.SimpleAssignmentExpression,
                    IdentifierName(name),
                    initValue))
                                   .NormalizeWhitespace();

            InitializationStatements.Add(init);
        }