コード例 #1
0
        public static IEnumerable <SyntaxNode> BuildVariable(this RoslynEcsTranslator translator, IVariableModel v, IPortModel portModel)
        {
            if (v is IConstantNodeModel model)
            {
                if (model.ObjectValue != null)
                {
                    yield return(translator.Constant(model.ObjectValue, translator.Stencil, model.Type));
                }

                yield break;
            }

            if (translator.InMacro.Count > 0 && v.DeclarationModel.VariableType == VariableType.GraphVariable && v.DeclarationModel.Modifiers == ModifierFlags.ReadOnly)
            {
                MacroRefNodeModel oldValue = translator.InMacro.Pop();

                var syntaxNodes = translator.BuildPort(oldValue.InputsById[v.DeclarationModel.VariableName]);
                translator.InMacro.Push(oldValue);
                foreach (var syntaxNode in syntaxNodes)
                {
                    yield return(syntaxNode);
                }
                yield break;
            }

            switch (v.DeclarationModel.VariableType)
            {
            case VariableType.GraphVariable:
                yield return(translator.context.GetSingletonVariable(v.DeclarationModel));

                break;

            case VariableType.FunctionVariable:
            case VariableType.ComponentGroupField:
                yield return(RoslynBuilder.LocalVariableReference(v.DeclarationModel.VariableName));

                break;

            case VariableType.FunctionParameter:
                var variableDeclarationModel = v.DeclarationModel;
                if (variableDeclarationModel.VariableIsAGeneratedEcsComponent(out var groupDeclaration))
                {
                    var relevantContext = translator.FindContext(groupDeclaration);
                    translator.context.RecordComponentAccess(relevantContext, v.DeclarationModel.DataType, translator.IsRecordingComponentAccesses);
                    yield return(RoslynBuilder.ArgumentReference(translator.context.GetComponentVariableName(groupDeclaration, variableDeclarationModel.DataType)));
                }
                else
                {
                    yield return(RoslynBuilder.ArgumentReference(v.DeclarationModel.VariableName));
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #2
0
        internal static void BuildCriteria(this IEntityManipulationTranslator self, RoslynEcsTranslator translator,
                                           TranslationContext context, ExpressionSyntax entity, IEnumerable <CriteriaModel> criteriaModels,
                                           StatementSyntax conditionBreak)
        {
            var criteriaList = criteriaModels.ToList();

            if (!criteriaList.Any())
            {
                return;
            }

            ExpressionSyntax ifExpressionSyntax = null;

            foreach (var model in criteriaList)
            {
                ExpressionSyntax finalExpression = null;
                foreach (var criterion in model.Criteria)
                {
                    if (!(criterion is Criterion componentCriterion))
                    {
                        continue;
                    }

                    var rightValue = componentCriterion.Value is ConstantNodeModel constantNodeModel
                        ? translator.Constant(constantNodeModel.ObjectValue, translator.Stencil, constantNodeModel.Type)
                        : IdentifierName(componentCriterion.Value.DeclarationModel.VariableName);

                    var expression = BinaryExpression(
                        componentCriterion.Operator.ToSyntaxKind(),
                        GetLeftValueFromCriterion(self, context, translator.Stencil, entity, componentCriterion),
                        rightValue) as ExpressionSyntax;

                    // TODO : Temporary. Once Unity.Mathematics have IComparable interface, remove this
                    // and use IComparable and IEquatable methods instead of operators
                    var rightValueType = GetRightValueType(componentCriterion, translator.Stencil);
                    if (rightValueType.Namespace != null && rightValueType.Namespace.StartsWith("Unity.Mathematics"))
                    {
                        expression = RoslynBuilder.MethodInvocation(
                            nameof(math.all),
                            typeof(math).ToTypeSyntax(),
                            new[] { Argument(expression) },
                            Enumerable.Empty <TypeSyntax>());
                    }

                    finalExpression = finalExpression == null
                        ? expression
                        : BinaryExpression(SyntaxKind.LogicalAndExpression, finalExpression, expression);
                }

                if (finalExpression == null)
                {
                    continue;
                }
                context.AddStatement(RoslynBuilder.DeclareLocalVariable(
                                         typeof(bool),
                                         model.Name,
                                         finalExpression,
                                         RoslynBuilder.VariableDeclarationType.InferredType));

                var unaryExpression = PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, IdentifierName(model.Name));
                if (model != criteriaList.First())
                {
                    ifExpressionSyntax = BinaryExpression(
                        SyntaxKind.LogicalAndExpression,
                        ifExpressionSyntax,
                        unaryExpression);
                }
                else
                {
                    ifExpressionSyntax = unaryExpression;
                }
            }

            if (ifExpressionSyntax != null)
            {
                BlockSyntax statementSyntax;
                if ((translator.Options & CompilationOptions.Tracing) != 0 && context is JobContext jobContext)
                {
                    statementSyntax = Block(List <SyntaxNode>()
                                            .Add(jobContext.MakeTracingEndForEachIndexStatement())
                                            .Add(conditionBreak));
                }
                else
                {
                    statementSyntax = Block(SingletonList(conditionBreak));
                }
                context.AddStatement(IfStatement(ifExpressionSyntax, statementSyntax));
            }
        }