Ejemplo n.º 1
0
 internal override Node Bind(Binder b)
 {
     b.Bind(ref Expr);
     b.Bind(ref Type, BindAffinity.Type);
     Expr.RequireGetAccess();
     Type.RequireType();
     Symbol = Type.Symbol as TypeSymbol;
     if (Expr.Datatype.IsValueType)
     {
         if (Binder.TypesMatch(Expr.Datatype, Type.Symbol as TypeSymbol))
         {
             return(LiteralExpr.Bound(Constant.Create(true)));
         }
         else if (Binder.TypesMatch(Type.Symbol as TypeSymbol, Compilation.Get(WellKnownTypes.System_ValueType)))
         {
             return(LiteralExpr.Bound(Constant.Create(true)));
         }
         else if (Binder.TypesMatch(Type.Symbol as TypeSymbol, Compilation.Get(NativeType.Object)))
         {
             return(LiteralExpr.Bound(Constant.Create(true)));
         }
         return(LiteralExpr.Bound(Constant.Create(false)));
     }
     Datatype = Compilation.Get(NativeType.Boolean);
     return(null);
 }
Ejemplo n.º 2
0
 internal static AliasExpr Bound(string fieldName)
 {
     return(new AliasExpr(null, LiteralExpr.Bound(Constant.Create(fieldName)), Token.None)
     {
         Datatype = Compilation.Get(NativeType.Usual)
     });
 }
Ejemplo n.º 3
0
        internal override Node Bind(Binder b)
        {
            b.OpenScope();
            if (Expr != null)
            {
                b.Bind(ref Expr);
                Expr.RequireGetAccess();
            }
            else
            {
                b.Bind(ref Decl);
            }
            b.Bind(ref Stmt);
            b.CloseScope();

            var u     = Decl != null ? Decl.VarDecls[0].Var : b.AddLocal(Expr.Datatype);
            var udecl = Decl ?? DeclStmt.Bound(VarDecl.Bound(u, Expr, b.Options.Binding));

            Expr du = AsTypeExpr.Bound(IdExpr.Bound(u), IdExpr.Bound(Compilation.Get(WellKnownTypes.System_IDisposable)));

            b.Cache(ref du);
            var cond = BinaryExpr.Bound(du, Expr.Token, LiteralExpr.Bound(Constant.Null), BinaryOperatorKind.NotEqual, b.Options.Binding);
            var exit = IfStmt.Bound(cond,
                                    ExprStmt.Bound(MethodCallExpr.Bound(b, null, Compilation.Get(WellKnownMembers.System_IDisposable_Dispose), du, ArgList.Empty)),
                                    null);

            return(StmtBlock.Bound(udecl,
                                   TryStmt.Bound(b,
                                                 Stmt,
                                                 exit)));
        }
Ejemplo n.º 4
0
        internal override Node Bind(Binder b)
        {
            if (Token.Type == TokenType.LPARAMETERS || Token.Type == TokenType.PARAMETERS)
            {
                for (int i = 0; i < VarDecls.Length; i++)
                {
                    var argIdx     = b.Options.ArrayBase + i;
                    var paramArray = IdExpr.Bound((b.Entity as Script).ParamArray);
                    VarDecls[i].Initializer =
                        IifExpr.Bound(
                            BinaryExpr.Bound(ArrayLengthExpr.Bound(paramArray), Token, LiteralExpr.Bound(Constant.Create(i)), BinaryOperatorKind.GreaterThan, b.Options.Binding),
                            ArrayAccessExpr.Bound(paramArray, ArgList.Bound(LiteralExpr.Bound(Constant.Create(argIdx))), b),
                            DefaultExpr.Bound(b, b.ObjectType),
                            b.Options.Binding);
                }
            }

            // TODO: Handle STATIC
            if (Token.Type == TokenType.STATIC)
            {
                throw Error(ErrorCode.NotSupported, "STATIC");
            }

            for (int i = 0; i < VarDecls.Length; i++)
            {
                b.Bind(ref VarDecls[i]);
            }
            return(null);
        }
Ejemplo n.º 5
0
        internal override Node Bind(Binder b)
        {
            b.OpenScope();
            b.Bind(ref Key);
            Key.RequireGetAccess();
            if (Key.Datatype.IsValueType)
            {
                throw Error(ErrorCode.RequireReferenceType);
            }
            b.Bind(ref Stmt);
            b.CloseScope();

            var k = b.AddLocal(Compilation.Get(NativeType.Object));

            b.Convert(ref Key, Compilation.Get(NativeType.Object));
            var kdecl = DeclStmt.Bound(VarDecl.Bound(k, Key, b.Options.Binding));

            var l     = b.AddLocal(Compilation.Get(NativeType.Boolean));
            var ldecl = DeclStmt.Bound(VarDecl.Bound(l, LiteralExpr.Bound(Constant.Create(false)), b.Options.Binding));

            var enter = ExprStmt.Bound(MethodCallExpr.Bound(b, null, Compilation.Get(WellKnownMembers.System_Threading_Monitor_Enter), null, ArgList.Bound(IdExpr.Bound(k), IdExpr.Bound(l))));
            var exit  = IfStmt.Bound(IdExpr.Bound(l),
                                     ExprStmt.Bound(MethodCallExpr.Bound(b, null, Compilation.Get(WellKnownMembers.System_Threading_Monitor_Exit), null, ArgList.Bound(IdExpr.Bound(k)))),
                                     null);

            return(StmtBlock.Bound(kdecl, ldecl,
                                   TryStmt.Bound(b,
                                                 StmtBlock.Bound(enter, Stmt),
                                                 exit)));
        }
Ejemplo n.º 6
0
        internal override Node Bind(Binder b)
        {
            if (Expr != null)
            {
                b.Bind(ref Expr);
                Expr.RequireGetAccess();
            }
            else
            {
                if (b.Options.Dialect == XSharpDialect.FoxPro)
                {
                    Expr = LiteralExpr.Bound(Constant.Create(false));
                }
                else
                {
                    Expr = LiteralExpr.Bound(Constant.CreateDefault(Compilation.Get(NativeType.Usual)));
                }
            }
            var t    = IdExpr.Bound(Compilation.Get(WellKnownTypes.XSharp_Internal_WrappedException));
            var args = ArgList.Bound(Expr);

            Expr = CtorCallExpr.Bound(b, t, args);
            b.Convert(ref Expr, Compilation.Get(NativeType.Object));
            return(null);
        }
Ejemplo n.º 7
0
        internal static AutoVarExpr Bound(string varName)
        {
            var e = LiteralExpr.Bound(Constant.Create(varName));

            return(new AutoVarExpr(e)
            {
                Symbol = e.Symbol, Datatype = Compilation.Get(NativeType.Usual)
            });
        }
Ejemplo n.º 8
0
        internal override Node Bind(Binder b)
        {
            Symbol = b.Lookup(Name);
            if (Affinity != BindAffinity.Invoke && Affinity != BindAffinity.Type)
            {
                if (Symbol.IsMethodOrMethodGroup())
                {
                    // IdExpr can't be a method
                    // TODO (nvk): If delegates are supprted this needs to be revised!
                    Symbol = null;
                }
                else if (Symbol is TypeSymbol)
                {
                    Symbol = null;
                }
                else if (Symbol is NamespaceSymbol)
                {
                    Symbol = null;
                }
                else if (Symbol is SymbolList)
                {
                    Symbol = Symbol.UniqueIdent();
                }
            }
            if (Symbol == null && Affinity != BindAffinity.Type)
            {
                if (Affinity == BindAffinity.Alias)
                {
                    return(LiteralExpr.Bound(Constant.Create(Name)));
                }
                else
                {
                    switch (b.Options.UndeclaredVariableResolution)
                    {
                    case VariableResolution.Error:
                        throw Error(ErrorCode.IdentifierNotFound, Name);

                    case VariableResolution.GenerateLocal:
                        Symbol = b.AddVariable(Name, Compilation.Get(NativeType.Usual));
                        break;

                    case VariableResolution.TreatAsField:
                        return(AliasExpr.Bound(Name));

                    case VariableResolution.TreatAsFieldOrMemvar:
                        if (Affinity == BindAffinity.Assign)
                        {
                            b.CreatesAutoVars = true;
                        }
                        return(AutoVarExpr.Bound(Name));
                    }
                }
            }
            Datatype = Symbol.Type();
            return(null);
        }
Ejemplo n.º 9
0
        internal override Node Bind(Binder b)
        {
            b.OpenScope();

            b.Bind(ref Expr);
            Expr.RequireGetAccess();

            if (Expr.Datatype.IsArray && Expr.Datatype.ArrayRank == 1)
            {
                var array = b.Cache(ref Expr);
                var iter  = b.AddLocal(Compilation.Get(NativeType.Int32));
                IterDecl  = VarDecl.Bound(iter, LiteralExpr.Bound(Constant.Create(b.Options.ArrayBase)), b.Options.Binding);
                WhileExpr = BinaryExpr.Bound(IdExpr.Bound(iter), Token,
                                             MethodCallExpr.Bound(array, Compilation.Get(WellKnownMembers.System_Array_get_Length), array, ArgList.Empty),
                                             b.Options.ArrayZero ? BinaryOperatorKind.LessThan : BinaryOperatorKind.LessThanOrEqual, b.Options.Binding);
                IncrExpr            = AssignOpExpr.Bound(IdExpr.Bound(iter), LiteralExpr.Bound(Constant.Create(1)), BinaryOperatorKind.Addition, b);
                ForDecl.Initializer = ArrayAccessExpr.Bound(array, new ArgList(new List <Arg>(1)
                {
                    new Arg(IdExpr.Bound(iter))
                }), b);
            }
            else
            {
                if (Expr.Datatype.IsUsualOrObject() && b.Options.Binding.HasFlag(BindOptions.AllowDynamic))
                {
                    b.Convert(ref Expr, Compilation.Get(NativeType.Array));
                }
                Expr e          = b.Cache(ref Expr);
                var  getIterSym = e.Datatype.GetEnumeratorGetter() ?? throw Error(ErrorCode.NoSuitableEnumerator);
                var  getIter    = MethodCallExpr.Bound(e, getIterSym, e, ArgList.Empty);
                var  iter       = b.AddLocal(getIter.Datatype);
                IterDecl  = VarDecl.Bound(iter, getIter, b.Options.Binding);
                WhileExpr = MethodCallExpr.Bound(b, IdExpr.Bound(iter), SystemNames.MoveNext, ArgList.Empty);
                b.Convert(ref WhileExpr, Compilation.Get(NativeType.Boolean));
                ForDecl.Initializer = MethodCallExpr.Bound(b, IdExpr.Bound(iter), SystemNames.CurrentGetter, ArgList.Empty);
                Dispose             = Compilation.Get(WellKnownTypes.System_IDisposable).IsAssignableFrom(getIter.Datatype);
                if (Dispose)
                {
                    RequireExceptionHandling = true;
                }
            }

            b.Bind(ref ForDecl);
            InnerDecl = ForDecl;

            Expr Iter;

            Iter = (ForDecl.Initializer as AssignExpr).Left;
            Iter.RequireGetAccess();

            b.Bind(ref Stmt);

            b.CloseScope();
            return(null);
        }
Ejemplo n.º 10
0
 internal override Node Bind(Binder b)
 {
     b.Bind(ref Type, BindAffinity.Type);
     Type.RequireType();
     if (b.Options.Dialect == XSharpDialect.FoxPro && (Type.Symbol as TypeSymbol).NativeType == NativeType.Usual)
     {
         return(LiteralExpr.Bound(Constant.Create(false)));
     }
     Symbol   = Type.Symbol as TypeSymbol;
     Datatype = Symbol as TypeSymbol;
     return(null);
 }
Ejemplo n.º 11
0
 internal static Expr Bound(Binder b, TypeSymbol type)
 {
     if (b.Options.Dialect == XSharpDialect.FoxPro && type.NativeType == NativeType.Usual)
     {
         Expr e = LiteralExpr.Bound(Constant.Create(false));
         b.Convert(ref e, type);
         return(e);
     }
     return(new DefaultExpr(null, null)
     {
         Symbol = type, Datatype = type
     });
 }
Ejemplo n.º 12
0
 internal static Expr Bound(Expr expr, TypeExpr type)
 {
     if (expr.Datatype.IsValueType)
     {
         if (Binder.TypesMatch(expr.Datatype, type.Symbol as TypeSymbol))
         {
             return(LiteralExpr.Bound(Constant.Create(true)));
         }
         else if (Binder.TypesMatch(type.Symbol as TypeSymbol, Compilation.Get(WellKnownTypes.System_ValueType)))
         {
             return(LiteralExpr.Bound(Constant.Create(true)));
         }
         else if (Binder.TypesMatch(type.Symbol as TypeSymbol, Compilation.Get(NativeType.Object)))
         {
             return(LiteralExpr.Bound(Constant.Create(true)));
         }
         return(LiteralExpr.Bound(Constant.Create(false)));
     }
     return(new IsExpr(expr, type, null)
     {
         Symbol = type.Symbol, Datatype = Compilation.Get(NativeType.Boolean)
     });
 }
Ejemplo n.º 13
0
 internal override Node Bind(Binder b)
 {
     if (b.Options.FoxParenArrayAccess && Expr is IdExpr id && (Args.Args.Count == 1 || Args.Args.Count == 2))
     {
         // Todo:
         // Validate also if the array indexes are or could be numeric
         // So valid are:
         // LONG, USUAL, OBJECT
         // When not, then we call the function always
         if (Affinity == BindAffinity.Assign)
         {
             // transform to array access
             var a = new ArrayAccessExpr(Expr, Args);
             b.Bind(ref a, Affinity);
             return(a);
         }
         else
         {
             Expr e = new IdExpr(id.Name);
             b.Bind(ref e, BindAffinity.Invoke);
             if (e.Symbol?.IsMethodOrMethodGroup() == true)
             {
                 if (b.Options.UndeclaredVariableResolution == VariableResolution.TreatAsFieldOrMemvar)
                 {
                     // transform to call to __FoxArrayAccess()
                     var m = (Args.Args.Count == 1) ? WellKnownMembers.XSharp_VFP_Functions___FoxArrayAccess_1 : WellKnownMembers.XSharp_VFP_Functions___FoxArrayAccess_2;
                     Args.Args.Insert(0, new Arg(LiteralExpr.Bound(Constant.Create(id.Name))));
                     Args.Args.Insert(1, new Arg(new IdExpr(id.Name)));
                     b.Bind(ref Args);
                     if (Args.Args[1].Expr is AutoVarExpr av)
                     {
                         av.Safe = true;
                     }
                     return(MethodCallExpr.Bound(b, null, Compilation.Get(m), null, Args));
                 }
             }
             else
             {
                 // transform to array access
                 var a = new ArrayAccessExpr(Expr, Args);
                 b.Bind(ref a, Affinity);
                 return(a);
             }
         }
     }
     b.Bind(ref Expr, BindAffinity.Invoke);
     b.Bind(ref Args);
     Symbol   = b.BindMethodCall(Expr, Expr.Symbol, Args, out Self, out WriteBack);
     Datatype = Symbol.Type();
     if (Self?.Datatype.IsValueType == true)
     {
         if ((Symbol as MethodSymbol).DeclaringType.IsValueType)
         {
             if (!Symbol.HasRefAccess)
             {
                 b.Cache(ref Self);
             }
             b.Convert(ref Self, Binder.ByRefOf(Self.Datatype));
         }
         else
         {
             b.Convert(ref Self, Compilation.Get(NativeType.Object));
         }
     }
     return(null);
 }
Ejemplo n.º 14
0
        internal override Node Bind(Binder b)
        {
            b.OpenScope();

            if (AssignExpr != null)
            {
                b.Bind(ref AssignExpr);
                IterInit = AssignExpr;
            }
            else
            {
                b.Bind(ref ForDecl);
                IterDecl   = ForDecl;
                AssignExpr = ForDecl.Initializer as AssignExpr;
            }

            Expr Iter;

            Iter = AssignExpr.Left;
            Iter.RequireGetAccess();

            b.Bind(ref Final);
            Final.RequireGetAccess();

            if (Step != null)
            {
                b.Bind(ref Step);
                Step.RequireGetAccess();
            }
            else
            {
                Step = LiteralExpr.Bound(Constant.Create(1));
            }

            switch (Dir.Type)
            {
            case TokenType.UPTO:
                WhileExpr = BinaryExpr.Bound(Iter, Dir, Final, BinaryOperatorKind.LessThanOrEqual, b.Options.Binding);
                IncrExpr  = AssignOpExpr.Bound(Iter, Step, BinaryOperatorKind.Addition, b);
                break;

            case TokenType.DOWNTO:
                WhileExpr = BinaryExpr.Bound(Iter, Dir, Final, BinaryOperatorKind.GreaterThanOrEqual, b.Options.Binding);
                IncrExpr  = AssignOpExpr.Bound(Iter, Step, BinaryOperatorKind.Subtraction, b);
                break;

            case TokenType.TO:
                var step_pos        = BinaryExpr.Bound(Step, Dir, LiteralExpr.Bound(Constant.Create(0)), BinaryOperatorKind.GreaterThanOrEqual, b.Options.Binding);
                var whileExprUpTo   = BinaryExpr.Bound(Iter, Dir, Final, BinaryOperatorKind.LessThanOrEqual, b.Options.Binding);
                var whileExprDownTo = BinaryExpr.Bound(Iter, Dir, Final, BinaryOperatorKind.GreaterThanOrEqual, b.Options.Binding);
                WhileExpr = IifExpr.Bound(step_pos, whileExprUpTo, whileExprDownTo, b.Options.Binding);
                IncrExpr  = AssignOpExpr.Bound(Iter, Step, BinaryOperatorKind.Addition, b);
                break;

            default:
                throw Error(ErrorCode.Internal);
            }
            b.Convert(ref WhileExpr, Compilation.Get(NativeType.Boolean));

            b.Bind(ref Stmt);

            b.CloseScope();
            return(null);
        }