public Expression CompileImplicitCast(Source src, string expectedType, Expression value)
        {
            var result = TryCompileImplicitCast(src, ILFactory.GetType(src, expectedType), value, true);

            ImplicitCastStack.RemoveLast();
            return(result);
        }
Exemple #2
0
        public TestSetupTransform(CompilerPass parent)
            : base(parent)
        {
            if (Environment.Options.TestOptions == null)
            {
                throw new InvalidOperationException("Internal error, could not get test options");
            }

            _testOptions         = Environment.Options.TestOptions.Value;
            _testAttributeType   = ILFactory.GetType("Uno.Testing.TestAttribute");
            _ignoreAttributeType = ILFactory.GetType("Uno.Testing.IgnoreAttribute");

            if (_testAttributeType is InvalidType)
            {
                throw new InvalidOperationException("Did you forget to reference the Uno.Testing package?");
            }

            _actionType    = (DelegateType)ILFactory.GetType("Uno.Action");
            _testSetupType = ILFactory.GetType("Uno.Testing.TestSetup");

            _appClass = Essentials.Application;

            _source           = Package.Source;
            _testRegistryType = ILFactory.GetType("Uno.Testing.Registry");
            _mainClass        = new ClassType(_source, Data.IL, null, Modifiers.Generated | Modifiers.Public, "MainClass");
            _mainClass.SetBase(_appClass);
            _mainConstructor = new Constructor(_source, _mainClass, null, Modifiers.Public, ParameterList.Empty);
            _mainClass.Constructors.Add(_mainConstructor);

            _testSetupField = new Field(_source, _mainClass, "_testSetup", null, Modifiers.Private, FieldModifiers.ReadOnly, _testSetupType);
            _mainClass.Fields.Add(_testSetupField);

            Data.IL.Types.Add(_mainClass);
        }
Exemple #3
0
        Expression ProcessVertexBuffer(StageValue vertexBuffer)
        {
            Expression result;

            if (!VertexBuffers.TryGetValue(vertexBuffer.Value.ToString(), out result))
            {
                var src  = vertexBuffer.Value.Source;
                var type = ILFactory.GetType(src, "Uno.Graphics.VertexBuffer");

                if (vertexBuffer.Value.ReturnType.Equals(type))
                {
                    result = ProcessStage(vertexBuffer, MetaStage.Volatile, MetaStage.Volatile).Value;
                    VertexBuffers.Add(vertexBuffer.Value.ToString(), result);
                    return(result);
                }

                var loc = LocationStack.Last();
                var mp  = GetProperty(loc);

                var name  = CreateFieldName(mp, loc);
                var owner = Path.DrawBlock.Method.DeclaringType;

                var field = new Field(src, owner, name, null, Modifiers.Private | Modifiers.Generated, 0, type);
                owner.Fields.Add(field);

                result = new LoadField(src, new This(src, owner), field);
                VertexBuffers.Add(vertexBuffer.Value.ToString(), result);

                if (vertexBuffer.MinStage > MetaStage.Volatile)
                {
                    Log.Error(src, ErrorCode.E5025, "Vertex buffer cannot be accessed from " + vertexBuffer.MinStage + " stage");
                    return(result);
                }
                else if (vertexBuffer.MinStage == MetaStage.Volatile)
                {
                    InitScope.Statements.Add(
                        new StoreField(src, new This(src, owner), field,
                                       ILFactory.NewObject(src, "Uno.Graphics.VertexBuffer",
                                                           ILFactory.GetExpression(src, "Uno.Graphics.BufferUsage.Dynamic"))));

                    FrameScope.Statements.Add(
                        ILFactory.CallMethod(src, new LoadField(src, new This(src, owner), field), "Update",
                                             vertexBuffer.Value));
                }
                else
                {
                    InitScope.Statements.Add(
                        new StoreField(src, new This(src, owner), field,
                                       ILFactory.NewObject(src, "Uno.Graphics.VertexBuffer",
                                                           vertexBuffer.Value,
                                                           ILFactory.GetExpression(src, "Uno.Graphics.BufferUsage.Immutable"))));
                }

                FreeScope.Statements.Add(
                    ILFactory.CallMethod(src, new LoadField(src, new This(src, owner), field), "Dispose"));
            }

            return(result);
        }
Exemple #4
0
 Expression CreateByteArray(Expression buf)
 {
     return(ILFactory.CallMethod(
                buf.ReturnType.Equals(ILFactory.GetType(buf.Source, "Uno.Buffer")) ?
                buf :
                ILFactory.CallMethod(buf.Source, "Uno.Runtime.Implementation.Internal.BufferConverters", "ToBuffer", buf),
                "GetBytes"));
 }
Exemple #5
0
 public GLGenerator(GLBackend backend, IBundle bundle)
     : base(backend)
 {
     _backend     = backend;
     _bundle      = bundle;
     _obfuscator  = new ShaderObfuscator(backend);
     _stringArray = (RefArrayType)ILFactory.GetType("string[]");
     _dumpShaders = Environment.IsDefined("DUMP_SHADERS");
 }
Exemple #6
0
 public override void Configure()
 {
     _outputDir = Environment.Combine(
         Environment.ExpandSingleLine("@(AssemblyDirectory || '.')")).TrimPath();
     _linker = new CilLinker(Log, Essentials);
     Scheduler.AddTransform(new CilTransform(this));
     EnableReflection   = Environment.IsDefined("REFLECTION");
     TypeAliasAttribute = EnableReflection
                         ? ILFactory.GetType("Uno.Reflection.TypeAliasAttribute")
                         : DataType.Invalid;
 }
Exemple #7
0
 void EnsureInitialized()
 {
     if (!_initialized)
     {
         ExpandInterceptor    = new ExpandInterceptor(InterceptEntity);
         BoxedJavaObject      = ILFactory.GetType("Java.Object");
         _initialized         = true;
         Convert              = new Converters.Converter(BoxedJavaObject, Essentials, ILFactory, Helpers);
         BlockHost            = new Entrypoints(Environment, Disk, ILFactory, Convert);
         UnoToJavaBoxingClass = ILFactory.GetType("Uno.Compiler.ExportTargetInterop.Foreign.Android.JavaUnoObject");
     }
 }
Exemple #8
0
 void EnsureInitialized(Source source, Function f)
 {
     Helpers.CacheContext(f, source);
     if (!_initialized)
     {
         _initialized   = true;
         _objCObject    = ILFactory.GetType("global::ObjC.Object");
         _objCID        = ILFactory.GetType("global::ObjC.ID");
         _intPtr        = ILFactory.GetType("global::Uno.IntPtr");
         _getHandle     = (Method)ILFactory.GetEntity("global::ObjC.Object.GetHandle(" + Helpers.FullGlobalName(_objCObject) + ")");
         _newObjCObject = (Method)ILFactory.GetEntity(Helpers.FullGlobalName(_objCObject) + ".Create(" + Helpers.FullGlobalName(_objCID) + ")");
     }
 }
Exemple #9
0
 internal void ResolveMainClass(CompilerPass parent, BuildEnvironment environment)
 {
     if (MainClass != null && MainClass != DataType.Invalid)
     {
         return;
     }
     if (!string.IsNullOrEmpty(environment.Options.MainClass))
     {
         SetMainClass(_ilf.GetType(environment.Options.MainClass));
     }
     else
     {
         new MainClassFinder(parent).Run();
     }
 }
Exemple #10
0
 public override void Configure()
 {
     SourceDirectory = Environment.GetOutputPath("SourceDirectory");
     HeaderDirectory = Environment.GetOutputPath("HeaderDirectory");
     Types.Add(Essentials.Bool, "bool");
     Types.Add(Essentials.Byte, "uint8_t");
     Types.Add(Essentials.Char, "char16_t");
     Types.Add(Essentials.Double, "double");
     Types.Add(Essentials.Float, "float");
     Types.Add(Essentials.Int, "int");
     Types.Add(Essentials.Long, "int64_t");
     Types.Add(Essentials.SByte, "int8_t");
     Types.Add(Essentials.Short, "int16_t");
     Types.Add(Essentials.UInt, "unsigned int");
     Types.Add(Essentials.ULong, "uint64_t");
     Types.Add(Essentials.UShort, "uint16_t");
     Types.Add(ILFactory.GetType("global::Uno.IntPtr"), "void*");
 }
Exemple #11
0
        void CompileTerminalProperty(TerminalProperty t)
        {
            var dt = _ilf.GetType(t.TypeString);
            var mp = new MetaProperty(Source.Unknown, _terminals, dt, t.Name, MetaVisibility.Public);
            var fc = new FunctionCompiler(_compiler, mp);

            _terminals.Members.Add(mp);
            _terminalNames.Add(mp.Name);

            if (t.DefaultString != null)
            {
                var e = Parser.ParseExpression(Log, Source.Unknown, t.DefaultString, ParseContext.MetaProperty);
                var v = fc.CompileExpression(e);
                mp.SetDefinitions(new MetaDefinition(v, new string[0]));
            }
            else
            {
                mp.SetDefinitions();
            }
        }
Exemple #12
0
 void EnsureInitialized()
 {
     if (!_initialized)
     {
         _initialized = true;
         IdentityTypes.Add(Essentials.Bool, "bool");
         IdentityTypes.Add(Essentials.Byte, "uint8_t");
         IdentityTypes.Add(Essentials.Char, "char16_t");
         IdentityTypes.Add(Essentials.Double, "double");
         IdentityTypes.Add(Essentials.Float, "float");
         IdentityTypes.Add(Essentials.Int, "int");
         IdentityTypes.Add(Essentials.Long, "int64_t");
         IdentityTypes.Add(Essentials.SByte, "int8_t");
         IdentityTypes.Add(Essentials.Short, "int16_t");
         IdentityTypes.Add(Essentials.UInt, "unsigned int");
         IdentityTypes.Add(Essentials.ULong, "uint64_t");
         IdentityTypes.Add(Essentials.UShort, "uint16_t");
         IdentityTypes.Add(ILFactory.GetType("global::Uno.IntPtr"), "void*");
     }
 }
Exemple #13
0
        public override void Initialize(ICompiler compiler, IBundle bundle)
        {
            base.Initialize(compiler, bundle);
            Scheduler.AddGenerator(new GLGenerator(this, bundle));
            GlslIntrinsicAttribute = ILFactory.GetType("Uno.Compiler.ExportTargetInterop.GlslIntrinsicAttribute");

            // GLSL types
            Types.Add(DataType.Void, "void");
            Types.Add(Essentials.Bool, "bool");
            Types.Add(Essentials.Byte, "float");   // No byte in GLES
            Types.Add(Essentials.Double, "float"); // No double in GLES
            Types.Add(Essentials.Int, "int");
            Types.Add(Essentials.Int2, "ivec2");
            Types.Add(Essentials.Int3, "ivec3");
            Types.Add(Essentials.Int4, "ivec4");
            Types.Add(Essentials.UInt, "uint");
            Types.Add(Essentials.Float, "float");
            Types.Add(Essentials.Float2, "vec2");
            Types.Add(Essentials.Float3, "vec3");
            Types.Add(Essentials.Float4, "vec4");
            Types.Add(Essentials.Float2x2, "mat2");
            Types.Add(Essentials.Float3x3, "mat3");
            Types.Add(Essentials.Float4x4, "mat4");
            Types.Add(Essentials.Sampler2D, "sampler2D");
            Types.Add(Essentials.SamplerCube, "samplerCube");
            Types.Add(Essentials.VideoSampler,
                      Environment.IsDefined("ANDROID")
                    ? "samplerExternalOES"
                    : "sampler2D");

            // GLSL functions
            AddFunction("float4x4.get_Item(int)");
            AddFunction("float4x4.set_Item(int,float4)");
            AddFunction("float3x3.get_Item(int)");
            AddFunction("float3x3.set_Item(int,float3)");
            AddFunction("float2x2.get_Item(int)");
            AddFunction("float2x2.set_Item(int,float2)");

            AddFunction("Uno.Matrix.Mul(float2x2,float2x2)",
                        (w, obj, args, p) =>
            {
                if (p >= ExpressionUsage.Operand)
                {
                    w.Write("(");
                }

                w.WriteExpression(args[1], ExpressionUsage.Operand);
                w.Write(w.Space + "*" + w.Space);
                w.WriteExpression(args[0], ExpressionUsage.Operand);

                if (p >= ExpressionUsage.Operand)
                {
                    w.Write(")");
                }
            });

            AddFunction("Uno.Matrix.Mul(float3x3,float3x3)",
                        (w, obj, args, p) =>
            {
                if (p >= ExpressionUsage.Operand)
                {
                    w.Write("(");
                }

                w.WriteExpression(args[1], ExpressionUsage.Operand);
                w.Write(w.Space + "*" + w.Space);
                w.WriteExpression(args[0], ExpressionUsage.Operand);

                if (p >= ExpressionUsage.Operand)
                {
                    w.Write(")");
                }
            });

            AddFunction("Uno.Matrix.Mul(float4x4,float4x4)",
                        (w, obj, args, p) =>
            {
                if (p >= ExpressionUsage.Operand)
                {
                    w.Write("(");
                }

                w.WriteExpression(args[1], ExpressionUsage.Operand);
                w.Write(w.Space + "*" + w.Space);
                w.WriteExpression(args[0], ExpressionUsage.Operand);

                if (p >= ExpressionUsage.Operand)
                {
                    w.Write(")");
                }
            });

            AddFunction("Uno.Vector.TransformNormal(float3,float4x4)",
                        (w, obj, args, p) =>
            {
                w.Write("(");
                w.WriteExpression(args[1], ExpressionUsage.Operand);
                w.Write(w.Space + "*" + w.Space);
                w.Write("vec4(");
                w.WriteExpression(args[0]);
                w.Write("," + w.Space + "0.)).xyz");
            });

            AddFunction("Uno.Vector.Transform(float4,float4x4)",
                        (w, obj, args, p) =>
            {
                if (p >= ExpressionUsage.Operand)
                {
                    w.Write("(");
                }

                w.WriteExpression(args[1], ExpressionUsage.Operand);
                w.Write(w.Space + "*" + w.Space);
                w.WriteExpression(args[0], ExpressionUsage.Operand);

                if (p >= ExpressionUsage.Operand)
                {
                    w.Write(")");
                }
            });

            AddFunction("Uno.Vector.Transform(float3,float4x4)",
                        (w, obj, args, p) =>
            {
                if (p >= ExpressionUsage.Operand)
                {
                    w.Write("(");
                }

                w.WriteExpression(args[1], ExpressionUsage.Operand);
                w.Write(w.Space + "*" + w.Space);
                w.Write("vec4(");
                w.WriteExpression(args[0]);
                w.Write("," + w.Space + "1.)");

                if (p >= ExpressionUsage.Operand)
                {
                    w.Write(")");
                }
            });

            AddFunction("Uno.Vector.Transform(float2,float4x4)",
                        (w, obj, args, p) =>
            {
                if (p >= ExpressionUsage.Operand)
                {
                    w.Write("(");
                }

                w.WriteExpression(args[1], ExpressionUsage.Operand);
                w.Write(w.Space + "*" + w.Space);
                w.Write("vec4(");
                w.WriteExpression(args[0]);
                w.Write("," + w.Space + "0.");
                w.Write("," + w.Space + "1.)");

                if (p >= ExpressionUsage.Operand)
                {
                    w.Write(")");
                }
            });

            AddFunction("Uno.Vector.TransformAffine(float3,float4x4)",
                        (w, obj, args, p) =>
            {
                w.Write("(");
                w.WriteExpression(args[1], ExpressionUsage.Operand);
                w.Write(w.Space + "*" + w.Space);
                w.Write("vec4(");
                w.WriteExpression(args[0]);
                w.Write("," + w.Space + "1.)).xyz");
            });

            AddFunction("Uno.Vector.Transform(float3,float3x3)",
                        (w, obj, args, p) =>
            {
                if (p >= ExpressionUsage.Operand)
                {
                    w.Write("(");
                }

                w.WriteExpression(args[1], ExpressionUsage.Operand);
                w.Write(w.Space + "*" + w.Space);
                w.WriteExpression(args[0], ExpressionUsage.Operand);

                if (p >= ExpressionUsage.Operand)
                {
                    w.Write(")");
                }
            });

            AddFunction("Uno.Vector.Transform(float2,float2x2)",
                        (w, obj, args, p) =>
            {
                if (p >= ExpressionUsage.Operand)
                {
                    w.Write("(");
                }

                w.WriteExpression(args[1], ExpressionUsage.Operand);
                w.Write(w.Space + "*" + w.Space);
                w.WriteExpression(args[0], ExpressionUsage.Operand);

                if (p >= ExpressionUsage.Operand)
                {
                    w.Write(")");
                }
            });

            AddFunction("Uno.Graphics.Sampler2D.Sample(float2)",
                        (w, obj, args, p) =>
            {
                w.Write("texture2D(");
                w.WriteExpression(obj);
                w.Write(w.Comma);
                w.WriteExpression(args[0]);
                w.Write(")");
            });

            AddFunction("Uno.Graphics.SamplerCube.Sample(float3)",
                        (w, obj, args, p) =>
            {
                w.Write("textureCube(");
                w.WriteExpression(obj);
                w.Write(w.Comma);
                w.WriteExpression(args[0]);
                w.Write(")");
            });

            AddFunction("Uno.Graphics.VideoSampler.Sample(float2)",
                        (w, obj, args, p) =>
            {
                w.Write("texture2D(");
                w.WriteExpression(obj);
                w.Write(w.Comma);
                w.WriteExpression(args[0]);
                w.Write(")");
            });
        }
Exemple #14
0
 public StructCopyTransform(CompilerPass parent)
     : base(parent)
 {
     DontCopyAttribute = ILFactory.GetType("Uno.Compiler.ExportTargetInterop.DontCopyStructAttribute");
 }
Exemple #15
0
        public Expression CompileExpression(AstExpression e)
        {
            switch (e.ExpressionType)
            {
            case AstExpressionType.Initializer:
            {
                var s    = (AstInitializer)e;
                var root = CompileExpression(s.Expressions[0]);
                for (var i = 1; i < s.Expressions.Count; i++)
                {
                    root = new SequenceOp(root, CompileExpression(s.Expressions[i]));
                }
                return(root);
            }

            case AstExpressionType.Identifier:
                return(CompilePartial(ResolveIdentifier(e as AstIdentifier, null)));

            case AstExpressionType.Macro:
                return(new ExternString(e.Source, Essentials.String, ((AstMacro)e).Value, GetUsings(e.Source)));

            case AstExpressionType.Void:
            case AstExpressionType.Global:
            case AstExpressionType.Generic:
            case AstExpressionType.BuiltinType:
            case AstExpressionType.Array:
            case AstExpressionType.FixedArray:
            case AstExpressionType.Parameterizer:
                return(CompilePartial(NameResolver.ResolveExpression(Namescope, e, null)));

            case AstExpressionType.Add:
            case AstExpressionType.Sub:
            case AstExpressionType.Mul:
            case AstExpressionType.Div:
            case AstExpressionType.Mod:
            case AstExpressionType.NullOp:
            case AstExpressionType.LogAnd:
            case AstExpressionType.LogOr:
            case AstExpressionType.Equal:
            case AstExpressionType.NotEqual:
            case AstExpressionType.LessThan:
            case AstExpressionType.LessThanOrEqual:
            case AstExpressionType.GreaterThan:
            case AstExpressionType.GreaterThanOrEqual:
            case AstExpressionType.BitwiseAnd:
            case AstExpressionType.BitwiseOr:
            case AstExpressionType.BitwiseXor:
            case AstExpressionType.ShiftLeft:
            case AstExpressionType.ShiftRight:
            case AstExpressionType.Assign:
            case AstExpressionType.AddAssign:
            case AstExpressionType.SubAssign:
            case AstExpressionType.MulAssign:
            case AstExpressionType.DivAssign:
            case AstExpressionType.ModAssign:
            case AstExpressionType.BitwiseAndAssign:
            case AstExpressionType.BitwiseOrAssign:
            case AstExpressionType.BitwiseXorAssign:
            case AstExpressionType.ShiftLeftAssign:
            case AstExpressionType.ShiftRightAssign:
            case AstExpressionType.LogAndAssign:
            case AstExpressionType.LogOrAssign:
            case AstExpressionType.Sequence:
                return(CompileBinOp(e as AstBinary));

            case AstExpressionType.DecreasePrefix:
            case AstExpressionType.DecreasePostfix:
            case AstExpressionType.IncreasePrefix:
            case AstExpressionType.IncreasePostfix:
            case AstExpressionType.Negate:
            case AstExpressionType.LogNot:
            case AstExpressionType.BitwiseNot:
                return(CompileUnOp(e as AstUnary));

            case AstExpressionType.Cast:
                return(CompileCast(e as AstCast));

            case AstExpressionType.Call:
                return(CompileCall(e as AstCall));

            case AstExpressionType.LookUp:
                return(CompilePartial(ResolveLookUp(e as AstCall)));

            case AstExpressionType.Member:
                return(CompilePartial(ResolveMember(e as AstMember, null)));

            case AstExpressionType.New:
                return(CompileNewExpression(e as AstNew));

            case AstExpressionType.This:
                return(CompileThis(e.Source));

            case AstExpressionType.Base:
                return(CompileBase(e.Source));

            case AstExpressionType.Import:
                return(CompileImport(e as AstImport));

            case AstExpressionType.VertexAttribImplicit:
                return(CompileVertexAttribImplicit(e as AstVertexAttribImplicit));

            case AstExpressionType.VertexAttribExplicit:
                return(CompileVertexAttribExplicit(e as AstVertexAttribExplicit));

            case AstExpressionType.Null:
                return(new Constant(e.Source, DataType.Null, null));

            case AstExpressionType.Defined:
                return(new Constant(e.Source, Essentials.Bool, Environment.Test(e.Source, (e as AstDefined).Condition)));

            case AstExpressionType.Default:
                return(new Default(e.Source, NameResolver.GetType(Namescope, ((AstUnary)e).Operand)));

            case AstExpressionType.True:
                return(new Constant(e.Source, Essentials.Bool, true));

            case AstExpressionType.False:
                return(new Constant(e.Source, Essentials.Bool, false));

            case AstExpressionType.Zero:
                return(new Constant(e.Source, Essentials.Int, 0));

            case AstExpressionType.Int:
                return(new Constant(e.Source, Essentials.Int, ((AstInt)e).Value));

            case AstExpressionType.UInt:
                return(new Constant(e.Source, Essentials.UInt, ((AstUInt)e).Value));

            case AstExpressionType.Long:
                return(new Constant(e.Source, Essentials.Long, ((AstLong)e).Value));

            case AstExpressionType.ULong:
                return(new Constant(e.Source, Essentials.ULong, ((AstULong)e).Value));

            case AstExpressionType.Float:
                return(new Constant(e.Source, Essentials.Float, ((AstFloat)e).Value));

            case AstExpressionType.Double:
                return(new Constant(e.Source, Essentials.Double, ((AstDouble)e).Value));

            case AstExpressionType.Char:
                return(new Constant(e.Source, Essentials.Char, ((AstChar)e).Value));

            case AstExpressionType.String:
                return(new Constant(e.Source, Essentials.String, ((AstString)e).Value));

            case AstExpressionType.Unchecked:
            {
                CheckCastStack.Add(false);
                var result = CompileExpression(((AstUnary)e).Operand);
                CheckCastStack.RemoveLast();
                return(result);
            }

            case AstExpressionType.ReadOnly:
            {
                var s = e as AstUnary;
                return(new StageOp(s.Source, MetaStage.ReadOnly, CompileExpression(s.Operand)));
            }

            case AstExpressionType.Volatile:
            {
                var s = e as AstUnary;
                return(new StageOp(s.Source, MetaStage.Volatile, CompileExpression(s.Operand)));
            }

            case AstExpressionType.Vertex:
            {
                var s = e as AstUnary;
                return(new StageOp(s.Source, MetaStage.Vertex, CompileExpression(s.Operand)));
            }

            case AstExpressionType.Pixel:
            {
                var s = e as AstUnary;
                return(new StageOp(s.Source, MetaStage.Pixel, CompileExpression(s.Operand)));
            }

            case AstExpressionType.Extern:
            {
                var s = (AstExtern)e;
                return(new ExternOp(s.Value.Source,
                                    Compiler.CompileAttributes(Namescope, s.Attributes),
                                    s.OptionalType != null
                            ? NameResolver.GetType(Namescope, s.OptionalType)
                            : DataType.Void,
                                    s.Value.String,
                                    ExtensionTransform.CreateObject(s.Source, Function, TypeBuilder.Parameterize(Function.DeclaringType)),
                                    s.OptionalArguments != null
                            ? CompileArgumentList(s.OptionalArguments)
                            : ExtensionTransform.CreateArgumentList(s.Source, Function),
                                    GetUsings(s.Source)));
            }

            case AstExpressionType.Ternary:
            {
                var s = e as AstTernary;

                if (s.Condition is AstDefined)
                {
                    var def    = s.Condition as AstDefined;
                    var result = Environment.Test(def.Source, def.Condition)
                            ? CompileExpression(s.True)
                            : CompileExpression(s.False);
                    return(result.ReturnType.IsNull
                            ? Error(s.Source, ErrorCode.E0000, "Cannot resolve type of <null> in constant folded conditional expression")
                            : result);
                }

                var cond  = CompileImplicitCast(s.Condition.Source, Essentials.Bool, CompileExpression(s.Condition));
                var left  = CompileExpression(s.True);
                var right = CompileExpression(s.False);

                if (!left.ReturnType.IsNull)
                {
                    right = CompileImplicitCast(s.False.Source, left.ReturnType, right);
                }
                else if (!right.ReturnType.IsNull)
                {
                    left = CompileImplicitCast(s.True.Source, right.ReturnType, left);
                }
                else
                {
                    return(Error(s.Source, ErrorCode.E2086, "The type of the conditional expression could not be resolved because there is no implicit conversion between <null> and <null>"));
                }

                return(new ConditionalOp(s.Source, cond, left, right));
            }

            case AstExpressionType.Local:
            {
                var s = e as AstLocal;
                var p = TryResolveLocalIdentifier(s.Name);

                if (p == null && Namescope is BlockBase)
                {
                    p = TryResolveCapturedLocalIdentifier(Namescope as BlockBase, s.Name);
                }

                return(p != null
                        ? CompilePartial(p)
                        : Error(e.Source, ErrorCode.E0000, s.Name.Quote() + " is not a local variable or method parameter"));
            }

            case AstExpressionType.Prev:
            {
                var s = e as AstPrev;

                if (s.OptionalName != null)
                {
                    var b = CompileExpression(s.OptionalName);
                    if (b.IsInvalid)
                    {
                        return(b);
                    }
                    var mp = b as GetMetaProperty;
                    return(mp == null
                            ? Error(s.OptionalName.Source, ErrorCode.E2035, b.Quote() + " is not a meta property")
                            : new GetMetaProperty(s.Source, mp.ReturnType, mp.Name, mp.Offset + s.Offset));
                }

                return(MetaProperty != null
                        ? new GetMetaProperty(s.Source, MetaProperty.ReturnType, MetaProperty.Name, 1)
                        : Error(s.Source, ErrorCode.E2036, "Current scope is not a meta property scope"));
            }

            case AstExpressionType.PixelSampler:
            {
                var s       = e as AstPixelSampler;
                var texture = CompileExpression(s.Texture);

                Expression samplerState = null;
                if (s.OptionalState != null)
                {
                    var samplerStateType = ILFactory.GetType(s.OptionalState.Source, "Uno.Graphics.SamplerState");
                    samplerState = CompileImplicitCast(s.OptionalState.Source, samplerStateType, CompileExpression(s.OptionalState));
                }

                switch (texture.ReturnType.BuiltinType)
                {
                case BuiltinType.Texture2D:
                    return(new NewPixelSampler(s.Source, Essentials.Sampler2D, texture, samplerState));

                case BuiltinType.TextureCube:
                    return(new NewPixelSampler(s.Source, Essentials.SamplerCube, texture, samplerState));

                case BuiltinType.VideoTexture:
                    return(new NewPixelSampler(s.Source, Essentials.VideoSampler, texture, samplerState));
                }

                return(!texture.IsInvalid
                        ? Error(s.Texture.Source, ErrorCode.E0000, "First argument to 'pixel_sampler' must be an object of type 'texture2D' or 'textureCube'")
                        : Expression.Invalid);
            }

            case AstExpressionType.Is:
            {
                var s    = (AstBinary)e;
                var obj  = CompileExpression(s.Left);
                var type = NameResolver.GetType(Namescope, s.Right);
                return(new IsOp(e.Source, obj, type, Essentials.Bool));
            }

            case AstExpressionType.As:
            {
                var s    = (AstBinary)e;
                var obj  = CompileExpression(s.Left);
                var type = NameResolver.GetType(Namescope, s.Right);
                return(new AsOp(e.Source, obj, type));
            }

            case AstExpressionType.SizeOf:
            {
                var s  = (AstUnary)e;
                var dt = NameResolver.GetType(Namescope, s.Operand);
                return(new Constant(s.Source, Essentials.Int, NameResolver.GetSizeOf(s.Source, dt)));
            }

            case AstExpressionType.TypeOf:
            {
                var s = (AstUnary)e;
                return(new TypeOf(s.Source, Essentials.Type, NameResolver.GetType(Namescope, s.Operand)));
            }

            case AstExpressionType.NameOf:
            {
                // TODO: Missing verification
                var s = (AstUnary)e;
                return(new Constant(s.Source, Essentials.String, ResolveExpression(s.Operand, null).ToString()));
            }

            case AstExpressionType.Lambda:
                return(CompileLambda((AstLambda)e));

            case AstExpressionType.ArrayInitializer:
                return(Error(e.Source, ErrorCode.E2075, "Array initializers are not supported in this context"));
            }

            return(e is AstIL
                ? (e as AstIL).Expression
                : Error(e.Source, ErrorCode.I2042, "Unhandled expression type in CompileExpression: " + e.ExpressionType));
        }
Exemple #16
0
        void ProcessIndexBuffer(Source vaSrc, StageValue indexBuffer, StageValue indexType)
        {
            if (IndexBuffer == null && indexBuffer.Value == null && IndexType == null && indexType.Value == null ||
                indexBuffer.Value != null && IndexBuffer == indexBuffer.Value.ToString() && indexType.Value != null && IndexType == indexType.Value.ToString())
            {
                // OK
                return;
            }

            if (IndexBuffer != null || indexBuffer.Value == null || IndexType != null || indexType.Value == null)
            {
                Log.Error(vaSrc, ErrorCode.E5023, "Index buffer argument must be consistent for all <vertex_attrib>s in " + Path.Quote());
                return;
            }

            var src  = indexBuffer.Value.Source;
            var type = ILFactory.GetType(src, "Uno.Graphics.IndexBuffer");

            IndexBuffer = indexBuffer.Value.ToString();
            IndexType   = indexType.Value.ToString();

            if (indexBuffer.Value.ReturnType.Equals(type))
            {
                DrawState.OptionalIndices = new IndexBinding(
                    ProcessStage(indexType, MetaStage.Volatile, MetaStage.Volatile).Value,
                    ProcessStage(indexBuffer, MetaStage.Volatile, MetaStage.Volatile).Value);

                return;
            }

            var loc = LocationStack.Last();
            var mp  = GetProperty(loc);

            var name  = CreateFieldName(mp, loc);
            var owner = Path.DrawBlock.Method.DeclaringType;

            var field = new Field(src, owner, name,
                                  null, Modifiers.Private | Modifiers.Generated, 0, type);

            owner.Fields.Add(field);

            DrawState.OptionalIndices = new IndexBinding(
                ProcessStage(indexType, MetaStage.Volatile, MetaStage.Volatile).Value,
                new LoadField(src, new This(src, owner), field));

            if (indexBuffer.MinStage > MetaStage.Volatile)
            {
                Log.Error(src, ErrorCode.E5024, "Index buffer cannot be accessed from " + indexBuffer.MinStage + " stage");
                return;
            }

            if (indexBuffer.MinStage == MetaStage.Volatile)
            {
                InitScope.Statements.Add(
                    new StoreField(src, new This(src, owner), field,
                                   ILFactory.NewObject(src, "Uno.Graphics.IndexBuffer",
                                                       ILFactory.GetExpression(src, "Uno.Graphics.BufferUsage.Dynamic"))));

                FrameScope.Statements.Add(
                    ILFactory.CallMethod(src, new LoadField(src, new This(src, owner), field), "Update",
                                         indexBuffer.Value));
            }
            else
            {
                InitScope.Statements.Add(
                    new StoreField(src, new This(src, owner), field, ILFactory.NewObject(src, "Uno.Graphics.IndexBuffer",
                                                                                         indexBuffer.Value,
                                                                                         ILFactory.GetExpression(src, "Uno.Graphics.BufferUsage.Immutable"))));
            }

            FreeScope.Statements.Add(
                ILFactory.CallMethod(src, new LoadField(src, new This(src, owner), field), "Dispose"));
        }
Exemple #17
0
        public void Generate()
        {
            // Verify and add path to meta property cache
            for (int i = 0; i < Path.Nodes.Length; i++)
            {
                for (int j = Path.Nodes[i].Top; j <= Path.Nodes[i].Bottom; j++)
                {
                    var mp  = (MetaProperty)Path.Nodes[i].Block.Members[j];
                    var loc = new MetaLocation(i, j);

                    MetaLocation ploc;
                    if (MetaProperties.TryGetValue(mp.Name, out ploc))
                    {
                        var pmp = GetProperty(ploc);


                        // TODO: This is not correct
                        if (!pmp.ReturnType.Equals(mp.ReturnType))
                        {
                            Log.Error(mp.Source, ErrorCode.E5000, mp.Name.Quote() + " does not have the same type as the previous declaration at " + pmp.Source + " when exposed from " + Path.Quote() + " at " + Path.Source);
                        }

                        PrevProperties.Add(loc, ploc);
                    }

                    MetaProperties[mp.Name] = loc;
                }
            }

            // Resolve terminal properties
            foreach (var tp in Backend.ShaderBackend.OutputProperties)
            {
                MetaLocation loc;
                if (!MetaProperties.TryGetValue(tp.Name, out loc))
                {
                    Log.Error(Path.Source, ErrorCode.I5001, "Terminal property " + tp.Name.Quote() + " was not found in " + Path.Quote());
                    continue;
                }

                var mp = GetProperty(loc);
                var dt = ILFactory.GetType(Path.Source, tp.TypeString);

                if (!mp.ReturnType.Equals(dt))
                {
                    Log.Error(Path.Source, ErrorCode.I5001, "Terminal property " + tp.Name.Quote() + " was found with type " + mp.ReturnType.Quote() + " when " + dt.Quote() + " was expected");
                    continue;
                }

                var sym = ProcessMetaProperty(loc, tp.Required);
                if (sym.Value == null)
                {
                    continue;
                }

                LocationStack.Add(loc);

                switch (tp.Stage)
                {
                case MetaStage.Vertex:
                    sym = ProcessStage(sym, MetaStage.Vertex, MetaStage.Vertex);
                    DrawState.VertexShader.Terminals[tp.Name] = sym.Value;
                    break;

                case MetaStage.Pixel:
                    sym = ProcessStage(sym, MetaStage.Pixel, MetaStage.Pixel);
                    DrawState.PixelShader.Terminals[tp.Name] = sym.Value;
                    break;

                default:
                    sym = ProcessStage(sym, MetaStage.Volatile, MetaStage.Volatile);
                    DrawState.Terminals[tp.Name] = sym.Value;
                    break;
                }

                LocationStack.RemoveLast();
            }

            if (!DrawState.Terminals.ContainsKey("VertexCount"))
            {
                var loc = MetaProperties["VertexCount"];
                var mp  = GetProperty(loc);

                if (DetectedVertexCounts.Count == 1)
                {
                    LocationStack.Add(loc);

                    var fc = new FunctionCompiler(Compiler, IL);
                    DrawState.Terminals["VertexCount"] = fc.CompileImplicitCast(DetectedVertexCounts[0].Value.Source, mp.ReturnType, ProcessStage(DetectedVertexCounts[0], MetaStage.Volatile, MetaStage.Volatile).Value);

                    LocationStack.RemoveLast();
                }
                else
                {
                    Log.Error(CreateTrace(mp, loc, null), ErrorCode.E5002, "Unable to auto detect 'VertexCount' in " + Path.Quote());
                }
            }

            MetaPropertyEmitter.Emit(this);

            ShaderProcessor.ProcessShader(this, DrawState.VertexShader);
            ShaderProcessor.ProcessShader(this, DrawState.PixelShader);

            ProcessStructs();

            var p = new IndirectionTransform(Compiler.Pass);

            DrawState.VertexShader.Visit(p);
            DrawState.PixelShader.Visit(p);
        }