示例#1
0
        private void EmitConstant(object value, Type type)
        {
            // Try to emit the constant directly into IL
            if (ILGen.CanEmitConstant(value, type))
            {
                _ilg.EmitConstant(value, type);
                return;
            }

            _boundConstants.EmitConstant(this, value, type);
        }
 protected internal override Expression VisitConstant(ConstantExpression node)
 {
     if (!this._inQuote)
     {
         if (ILGen.CanEmitConstant(node.Value, node.Type))
         {
             return(node);
         }
         this._constants.Peek().AddReference(node.Value, node.Type);
     }
     return(node);
 }
        //CONFORMING
        private void EmitConstant(object value, Type type)
        {
            // Try to emit the constant directly into IL
            if (ILGen.CanEmitConstant(value, type))
            {
                _ilg.EmitConstant(value, type);
                return;
            }

            Debug.Assert(_dynamicMethod); // constructor enforces this

            _boundConstants.EmitConstant(this, value, type);
        }
示例#4
0
        /// <summary>
        /// Emits a live object as a constant
        /// </summary>
        internal void EmitConstant(LambdaCompiler lc, object value, Type type)
        {
            Debug.Assert(!ILGen.CanEmitConstant(value, type));

            type = TypeUtils.GetConstantType(type);

            LocalBuilder local;

            if (_cache.TryGetValue(new TypedConstant(value, type), out local))
            {
                lc.IL.Emit(OpCodes.Ldloc, local);
                return;
            }
            EmitConstantsArray(lc);
            EmitConstantFromArray(lc, value, type);
        }
示例#5
0
        /// <summary>
        /// Emits a live object as a constant
        /// </summary>
        internal void EmitConstant(LambdaCompiler lc, object value, Type type)
        {
            Debug.Assert(!ILGen.CanEmitConstant(value, type));

            if (!lc.CanEmitBoundConstants)
            {
                throw Error.CannotCompileConstant(value);
            }

            if (_cache.TryGetValue(new TypedConstant(value, type), out var local))
            {
                lc.IL.Emit(OpCodes.Ldloc, local);
                return;
            }
            EmitConstantsArray(lc);
            EmitConstantFromArray(lc, value, type);
        }
示例#6
0
        protected internal override Expression VisitConstant(ConstantExpression node)
        {
            // If we're in Quote, we can ignore constants completely
            if (_inQuote)
            {
                return(node);
            }

            // Constants that can be emitted into IL don't need to be stored on
            // the delegate
            if (ILGen.CanEmitConstant(node.Value, node.Type))
            {
                return(node);
            }

            _constants.Peek().AddReference(node.Value, node.Type);
            return(node);
        }
示例#7
0
        internal void EmitConstant(LambdaCompiler lc, object?value, Type type)
        {
            Debug.Assert(!ILGen.CanEmitConstant(value, type));

            if (!lc.CanEmitBoundConstants)
            {
                throw new InvalidOperationException($"CompileToMethod cannot compile constant '{value}' because it is a non-trivial value, such as a live object. Instead, create an expression tree that can construct this value.");
            }

            if (_cache.TryGetValue(new TypedConstant(value, type), out var local))
            {
                lc.IL.Emit(OpCodes.Ldloc, local);
                return;
            }

            EmitConstantsArray(lc);
            EmitConstantFromArray(lc, value, type);
        }
示例#8
0
        /// <summary>
        /// Emits a live object as a constant
        /// </summary>
        internal void EmitConstant(LambdaCompiler lc, object value, Type type)
        {
            Debug.Assert(!ILGen.CanEmitConstant(value, type));

#if FEATURE_COMPILE_TO_METHODBUILDER
            if (!lc.CanEmitBoundConstants)
            {
                throw Error.CannotCompileConstant(value);
            }
#endif

            if (_cache.TryGetValue(new TypedConstant(value, type), out LocalBuilder? local))
            {
                lc.IL.Emit(OpCodes.Ldloc, local);
                return;
            }
            EmitConstantsArray(lc);
            EmitConstantFromArray(lc, value, type);
        }