Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
 public override string GetComponentVariableName(IIteratorStackModel query, TypeHandle componentVariableType)
 {
     // TODO forward to parent if necessary
     return(query == IterationContext.Query
         ? IterationContext.GetComponentDataName(componentVariableType.Resolve(IterationContext.Stencil))
         : Parent.GetComponentVariableName(query, componentVariableType));
 }
Ejemplo n.º 3
0
        public override ExpressionSyntax GetCachedValue(string key, ExpressionSyntax value, TypeHandle type,
                                                        params IdentifierNameSyntax[] attributes)
        {
            var constant = base.GetCachedValue(key, value, type, attributes);

            // Use the same identifier as the parentJob. This avoid things like myCoroutine.FixedTime0 = FixedTime
            var variableName = constant is IdentifierNameSyntax ins
                ? ins.Identifier.Text
                : key.Replace(".", "_");

            var found = m_Parameters.FirstOrDefault(p => p.Value.Identifier.Text.Equals(variableName));

            if (found.Value != null)
            {
                return(IdentifierName(found.Value.Identifier));
            }

            var param = Parameter(Identifier(variableName))
                        .WithType(TypeSystem.BuildTypeSyntax(type.Resolve(IterationContext.Stencil)));

            if (attributes.Any())
            {
                param.AddAttributeLists(AttributeList(SeparatedList(attributes.Select(Attribute))));
            }

            m_Parameters.Add(Argument(constant), param);

            return(IdentifierName(param.Identifier));
        }
        public virtual List <SearcherDatabase> GetTypeMembersSearcherDatabases(TypeHandle typeHandle)
        {
            if (typeHandle.GraphModelReference)
            {
                return(new List <SearcherDatabase>
                {
                    new GraphElementSearcherDatabase(m_Stencil)
                    .AddGraphAssetMembers(typeHandle.GraphModelReference)
                    .Build()
                });
            }

            // TODO : Need to be handled by TypeHandle.Resolve
            Type type = typeHandle == TypeHandle.ThisType
                ? m_Stencil.GetThisType().Resolve(m_Stencil)
                : typeHandle.Resolve(m_Stencil);

            return(new List <SearcherDatabase>
            {
                new GraphElementSearcherDatabase(m_Stencil)
                .AddMembers(
                    new[] { type },
                    MemberFlags.Field | MemberFlags.Method | MemberFlags.Property | MemberFlags.Extension,
                    BindingFlags.Instance | BindingFlags.Public
                    )
                .Build()
            });
        }
Ejemplo n.º 5
0
        public static Type InferReturnType(Stencil stencil, MethodBase methodBase, Type[] genericTypes, TypeHandle[] solvedTypeArguments, Type dataType)
        {
            Type returnType = methodBase.GetReturnType();

            if (returnType == typeof(void) || !returnType.ContainsGenericParameters)
            {
                return(null);
            }

            Type       genericType   = genericTypes.SingleOrDefault();
            TypeHandle solvedType    = solvedTypeArguments.SingleOrDefault();
            Type       newOutputType = null;

            if (returnType == genericType) // T F<T>() == T
            {
                newOutputType = solvedType.Resolve(stencil);
            }
            else if (returnType.IsGenericType)                                                                  // List<T> F<T>()
            {
                if (returnType.IsConstructedGenericType && returnType.GenericTypeArguments[0] == genericType || // class C<T> { List<T> F() }
                    !returnType.IsConstructedGenericType && returnType.GetGenericArguments()[0] == genericType) // class C { List<T> F<T>() }
                {
                    newOutputType = dataType.GetGenericTypeDefinition().MakeGenericType(solvedType.Resolve(stencil));
                }
            }

            return(newOutputType);
        }
Ejemplo n.º 6
0
        public void Test_TypeHandleDeserializationOfRegularType()
        {
            //Arrange
            TypeHandle th = typeof(A).GenerateTypeHandle(m_TypeSerializer);

            //Act
            Type deserializedTypeHandle = th.Resolve(m_TypeSerializer);

            //Assert
            Assert.That(deserializedTypeHandle, Is.EqualTo(typeof(A)));
        }
Ejemplo n.º 7
0
        public void Test_TypeHandleDeserializationOfRenamedType()
        {
            var typeSerializer = new CSharpTypeSerializer(new Dictionary <string, string>
            {
                { typeof(A).AssemblyQualifiedName, typeof(B).AssemblyQualifiedName }
            });

            TypeHandle th = typeof(A).GenerateTypeHandle(typeSerializer);

            Type deserializedTypeHandle = th.Resolve(typeSerializer);

            Assert.That(deserializedTypeHandle, Is.EqualTo(typeof(B)));
        }
Ejemplo n.º 8
0
        public void Test_TypeHandle_WithNestedType_Resolve_ChangedAssembly_WithMovedFromAttribute()
        {
            var typeStr         = typeof(EnclosingType.InnerTypeUnchanged).AssemblyQualifiedName;
            var originalTypeStr = typeStr?
                                  .Replace("NewNamespace", "OldNamespace")
                                  .Replace("GraphTools.", "OldAssemblyName.");

            var typeHandle = new TypeHandle(originalTypeStr);

            var resolvedType = typeHandle.Resolve();

            Assert.AreEqual(typeof(EnclosingType.InnerTypeUnchanged), resolvedType);
        }
Ejemplo n.º 9
0
        public void Test_TypeHandle_Resolve_WorksWithRenamedTypes_WithMovedFromAttribute()
        {
            var typeStr         = typeof(NewTypeName).AssemblyQualifiedName;
            var originalTypeStr = typeStr?
                                  .Replace("NewNamespace", "OldNamespace")
                                  .Replace("NewTypeName", "OldTypeName")
                                  .Replace("GraphTools.", "OldAssemblyName.");

            var typeHandle = new TypeHandle(originalTypeStr);

            var resolvedType = typeHandle.Resolve();

            Assert.AreEqual(typeof(NewTypeName), resolvedType);
        }
Ejemplo n.º 10
0
        public void Test_TypeHandle_WithNestedType_Resolve_WorksWithRenamedTypes_WithMovedFromAttribute()
        {
            var typeStr         = typeof(NewNamespace.EnclosingType.InnerNew).AssemblyQualifiedName;
            var originalTypeStr = typeStr
                                  .Replace("NewNamespace", "OldNamespace")
                                  .Replace("InnerNew", "InnerOld")
                                  .Replace("GraphTools", "OldAssemblyName");

            var typeHandle = new TypeHandle(originalTypeStr);

            var resolvedType = typeHandle.Resolve(m_TypeSerializer);

            Assert.AreEqual(typeof(NewNamespace.EnclosingType.InnerNew), resolvedType);
        }
        public bool HasValidOperationForInput(IPortModel port, TypeHandle typeHandle)
        {
            PortName portName  = ReferenceEquals(port, InputPortA) ? PortName.PortA : PortName.PortB;
            var      otherPort = portName == PortName.PortA ? InputPortB : InputPortA;
            var      dataType  = typeHandle.Resolve(Stencil);

            if (otherPort.Connected)
            {
                Type otherPortType = otherPort.DataType.Resolve(Stencil);

                return(portName == PortName.PortB
                    ? TypeSystem.IsBinaryOperationPossible(otherPortType, dataType, Kind)
                    : TypeSystem.IsBinaryOperationPossible(dataType, otherPortType, Kind));
            }

            return(TypeSystem.GetOverloadedBinaryOperators(dataType).Contains(Kind));
        }
        public void Test_TypeHandleDeserializationOfRenamedType_PositionTranslation()
        {
            var positionName = typeof(Translation).AssemblyQualifiedName.Replace("Translation", "Position");

            Debug.Log(positionName);
            var typeSerializer = new CSharpTypeSerializer(new Dictionary <string, string>
            {
                { positionName, typeof(Translation).AssemblyQualifiedName }
            });

            TypeHandle th = new TypeHandle {
                Identification = positionName
            };

            Type deserializedTypeHandle = th.Resolve(typeSerializer);

            Assert.That(deserializedTypeHandle, Is.EqualTo(typeof(Translation)));
        }
        /// <summary>
        /// Maps <see cref="TypeHandle"/> to a type of <see cref="IConstant"/>.
        /// </summary>
        public static Type GetConstantNodeType(TypeHandle typeHandle)
        {
            if (s_TypeToConstantNodeModelTypeCache == null)
            {
                s_TypeToConstantNodeModelTypeCache = new Dictionary <TypeHandle, Type>
                {
                    { TypeHandle.Bool, typeof(BooleanConstant) },
                    { TypeHandle.Double, typeof(DoubleConstant) },
                    { TypeHandle.Float, typeof(FloatConstant) },
                    { TypeHandle.Int, typeof(IntConstant) },
                    { TypeHandle.Quaternion, typeof(QuaternionConstant) },
                    { TypeHandle.String, typeof(StringConstant) },
                    { TypeHandle.Vector2, typeof(Vector2Constant) },
                    { TypeHandle.Vector3, typeof(Vector3Constant) },
                    { TypeHandle.Vector4, typeof(Vector4Constant) },
                    { typeof(Color).GenerateTypeHandle(), typeof(ColorConstant) },
                    { typeof(AnimationClip).GenerateTypeHandle(), typeof(AnimationClipConstant) },
                    { typeof(Mesh).GenerateTypeHandle(), typeof(MeshConstant) },
                    { typeof(Texture2D).GenerateTypeHandle(), typeof(Texture2DConstant) },
                    { typeof(Texture3D).GenerateTypeHandle(), typeof(Texture3DConstant) },
                };
            }

            if (s_TypeToConstantNodeModelTypeCache.TryGetValue(typeHandle, out var result))
            {
                return(result);
            }

            Type t = typeHandle.Resolve();

            if (t.IsEnum || t == typeof(Enum))
            {
                return(typeof(EnumConstant));
            }

            return(null);
        }
Ejemplo n.º 14
0
        public virtual bool HasValidOperationForInput(IPortModel _, TypeHandle typeHandle)
        {
            var type = typeHandle.Resolve(Stencil);

            return(TypeSystem.GetOverloadedUnaryOperators(type).Contains(Kind));
        }
Ejemplo n.º 15
0
 public override string GetComponentVariableName(IIteratorStackModel groupDeclaration, TypeHandle componentVariableType1)
 {
     Assert.AreEqual(groupDeclaration, IterationContext.Query);
     return(IterationContext.GetComponentDataName(componentVariableType1.Resolve(IterationContext.Stencil)));
 }
 public ITypeMetadata Resolve(TypeHandle th)
 {
     if (!m_MetadataCache.TryGetValue(th, out ITypeMetadata metadata))
     {
         metadata = m_MetadataCache.GetOrAdd(th, t => new CSharpTypeBasedMetadata(m_GraphContext, t, th.Resolve(m_GraphContext.CSharpTypeSerializer)));
     }
     return(metadata);
 }