Пример #1
0
            public Invocation GetCtorInvocation()
            {
                AST.Expression rr = LazyInit.VolatileRead(ref this.ctorInvocation);
                if (rr != null)
                {
                    return(rr as Invocation);
                }
                else
                {
                    int totalArgumentCount         = unresolved.positionalArguments.Count + unresolved.namedCtorArguments.Count;
                    AST.Expression[] arguments     = new AST.Expression[totalArgumentCount];
                    string[]         argumentNames = new string[totalArgumentCount];
                    int i = 0;
                    while (i < unresolved.positionalArguments.Count)
                    {
                        IConstantValue cv = unresolved.positionalArguments[i];
                        arguments[i] = cv.ResolveConstant(context);
                        i++;
                    }
                    foreach (var pair in unresolved.namedCtorArguments)
                    {
                        argumentNames[i] = pair.Key;
                        arguments[i]     = pair.Value.ResolveConstant(context);
                        i++;
                    }

                    rr = NewExpression.ResolveObjectCreation(context, unresolved.loc, attributeType, arguments, argumentNames);
                    return(LazyInit.GetOrSet(ref this.ctorInvocation, rr) as Invocation);
                }
            }
Пример #2
0
 void ISupportsInterning.PrepareForInterning(IInterningProvider provider)
 {
     name         = provider.Intern(name);
     type         = provider.Intern(type);
     attributes   = provider.InternList(attributes);
     defaultValue = provider.Intern(defaultValue);
 }
        public UnresolvedSecurityDeclarationBlob(int securityAction, byte[] blob)
        {
            BlobReader reader = new BlobReader(blob, null);

            this.securityAction = new SimpleConstantValue(securityActionTypeReference, securityAction);
            this.blob           = blob;
            if (reader.ReadByte() == '.')
            {
                // binary attribute
                uint attributeCount = reader.ReadCompressedUInt32();
                for (uint i = 0; i < attributeCount; i++)
                {
                    unresolvedAttributes.Add(new UnresolvedSecurityAttribute(this, (int)i));
                }
            }
            else
            {
                // for backward compatibility with .NET 1.0: XML-encoded attribute
                var attr = new DefaultUnresolvedAttribute(permissionSetAttributeTypeReference);
                attr.ConstructorParameterTypes.Add(securityActionTypeReference);
                attr.PositionalArguments.Add(this.securityAction);
                string xml = System.Text.Encoding.Unicode.GetString(blob);
                attr.AddNamedPropertyArgument("XML", new SimpleConstantValue(KnownTypeReference.String, xml));
                unresolvedAttributes.Add(attr);
            }
        }
Пример #4
0
        public IParameter CreateResolvedParameter(ITypeResolveContext context)
        {
            Freeze();

            if (DefaultExpression != null)
            {
                defaultValue = new CastExpression(this.type as Expression, DefaultExpression, DefaultExpression.Location);
            }


            if (defaultValue != null)
            {
                return(new ResolvedParameterWithDefaultValue(defaultValue, context)
                {
                    Type = type.Resolve(context),
                    Name = name,
                    Region = region,
                    Attributes = attributes.CreateResolvedAttributes(context),
                    IsRef = this.IsRef,
                    IsOut = this.IsOut,
                    IsParams = this.IsParams
                });
            }
            else
            {
                var  owner = context.CurrentMember as IParameterizedMember;
                var  resolvedAttributes = attributes.CreateResolvedAttributes(context);
                bool isOptional         = resolvedAttributes != null && resolvedAttributes.Any(a => IsOptionalAttribute(a.AttributeType));
                return(new ParameterSpec(type.Resolve(context), name, owner, region,
                                         resolvedAttributes, IsRef, IsOut, IsParams, isOptional));
            }
        }
Пример #5
0
            InvocationResolveResult GetCtorInvocation()
            {
                ResolveResult rr = LazyInit.VolatileRead(ref this.ctorInvocation);

                if (rr != null)
                {
                    return(rr as InvocationResolveResult);
                }
                else
                {
                    CSharpResolver  resolver           = new CSharpResolver(context);
                    int             totalArgumentCount = unresolved.positionalArguments.Count + unresolved.namedCtorArguments.Count;
                    ResolveResult[] arguments          = new ResolveResult[totalArgumentCount];
                    string[]        argumentNames      = new string[totalArgumentCount];
                    int             i = 0;
                    while (i < unresolved.positionalArguments.Count)
                    {
                        IConstantValue cv = unresolved.positionalArguments[i];
                        arguments[i] = cv.Resolve(context);
                        i++;
                    }
                    foreach (var pair in unresolved.namedCtorArguments)
                    {
                        argumentNames[i] = pair.Key;
                        arguments[i]     = pair.Value.Resolve(context);
                        i++;
                    }
                    rr = resolver.ResolveObjectCreation(attributeType, arguments, argumentNames);
                    return(LazyInit.GetOrSet(ref this.ctorInvocation, rr) as InvocationResolveResult);
                }
            }
Пример #6
0
 public void AddNamedFieldArgument(string fieldName, IConstantValue value)
 {
     this.NamedArguments.Add(new KeyValuePair <IMemberReference, IConstantValue>(
                                 new DefaultMemberReference(EntityType.Field, attributeType, fieldName),
                                 value
                                 ));
 }
Пример #7
0
        public override BoundNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
        {
            if (node.Initializer != null)
            {
                throw new NotSupportedException(LocStr.CE_InitializerListsNotSupported, node);
            }

            MethodSymbol constructorSymbol = (MethodSymbol)GetSymbol(node);

            BoundExpression[] boundArguments = new BoundExpression[node.ArgumentList.Arguments.Count];

            bool isConstant = true;

            for (int i = 0; i < boundArguments.Length; ++i)
            {
                boundArguments[i] = VisitExpression(node.ArgumentList.Arguments[i].Expression, constructorSymbol.Parameters[i].Type);
                isConstant       &= boundArguments[i].IsConstant;
            }

            // Constant folding on struct creation when possible
            // Also implicitly handles parameterless constructors on value types, which Udon does not expose constructors for
            if (isConstant && constructorSymbol.IsExtern && constructorSymbol.ContainingType.IsValueType)
            {
                var constArgs = boundArguments.Select(e => e.ConstantValue.Value).ToArray();

                object constantValue = Activator.CreateInstance(constructorSymbol.ContainingType.UdonType.SystemType, constArgs);

                IConstantValue constantStore = (IConstantValue)Activator.CreateInstance(typeof(ConstantValue <>).MakeGenericType(constantValue.GetType()), constantValue);

                return(new BoundConstantExpression(constantStore, constructorSymbol.ContainingType, node));
            }

            return(BoundInvocationExpression.CreateBoundInvocation(Context, node, constructorSymbol, null, boundArguments));
        }
Пример #8
0
 public void AddNamedPropertyArgument(string propertyName, IConstantValue value)
 {
     this.NamedArguments.Add(new KeyValuePair <IMemberReference, IConstantValue>(
                                 new DefaultMemberReference(EntityType.Property, attributeType, propertyName),
                                 value
                                 ));
 }
Пример #9
0
 public Expression ConvertConstantValue(IConstantValue constantValue)
 {
     if (constantValue == null)
     {
         throw new ArgumentNullException("constantValue");
     }
     return(ConvertConstantValue(constantValue.Resolve(context)));
 }
Пример #10
0
 public override void ApplyInterningProvider(IInterningProvider provider)
 {
     base.ApplyInterningProvider(provider);
     if (provider != null)
     {
         constantValue = provider.Intern(constantValue);
     }
 }
Пример #11
0
            public BoundPrefixOperatorExpression(AbstractPhaseContext context, SyntaxNode node, BoundAccessExpression assignmentTarget, MethodSymbol operatorMethod)
                : base(node, null, null, null)
            {
                TargetExpression = assignmentTarget;
                Type           targetType     = TargetExpression.ValueType.UdonType.SystemType;
                IConstantValue incrementValue = (IConstantValue)Activator.CreateInstance(
                    typeof(ConstantValue <>).MakeGenericType(targetType), Convert.ChangeType(1, targetType));

                InternalExpression = CreateBoundInvocation(context, null, operatorMethod, null,
                                                           new BoundExpression[] { assignmentTarget, new BoundConstantExpression(incrementValue, TargetExpression.ValueType, node) });
            }
Пример #12
0
 ResolveResult Resolve(IConstantValue constantValue, ITypeResolveContext context)
 {
     if (constantValue != null)
     {
         return(constantValue.Resolve(context));
     }
     else
     {
         return(new ErrorResolveResult(SharedTypes.UnknownType));
     }
 }
Пример #13
0
 /// <summary>
 /// Copy constructor
 /// </summary>
 public DefaultParameter(IParameter p)
 {
     this.name         = p.Name;
     this.type         = p.Type;
     this.attributes   = CopyList(p.Attributes);
     this.defaultValue = p.DefaultValue;
     this.region       = p.Region;
     this.IsRef        = p.IsRef;
     this.IsOut        = p.IsOut;
     this.IsParams     = p.IsParams;
 }
Пример #14
0
		/// <summary>
		/// Copy constructor
		/// </summary>
		public DefaultParameter(IParameter p)
		{
			this.name = p.Name;
			this.type = p.Type;
			this.attributes = CopyList(p.Attributes);
			this.defaultValue = p.DefaultValue;
			this.region = p.Region;
			this.IsRef = p.IsRef;
			this.IsOut = p.IsOut;
			this.IsParams = p.IsParams;
		}
Пример #15
0
        internal override Expression Resolve(ParserContext parser)
        {
            if (parser.ConstantAndEnumResolutionState[this.ConstStatement] != ConstantResolutionState.RESOLVED)
            {
                this.ConstStatement.Resolve(parser);
            }

            IConstantValue value = this.ConstStatement.Expression as IConstantValue;

            if (value == null)
            {
                throw new ParserException(this.ConstStatement, "Could not resolve this expression into a constant value.");
            }
            return(value.CloneValue(this.FirstToken, this.Owner));
        }
Пример #16
0
        public void FieldOffsetAttribute()
        {
            IField field = ctx.GetClass(typeof(ExplicitFieldLayoutStruct)).Fields.Single(f => f.Name == "Field0");

            Assert.AreEqual("System.Runtime.InteropServices.FieldOffsetAttribute", field.Attributes.Single().AttributeType.Resolve(ctx).FullName);
            IConstantValue arg = field.Attributes.Single().PositionalArguments.Single();

            Assert.AreEqual("System.Int32", arg.GetValueType(ctx).FullName);
            Assert.AreEqual(0, arg.GetValue(ctx));

            field = ctx.GetClass(typeof(ExplicitFieldLayoutStruct)).Fields.Single(f => f.Name == "Field100");
            Assert.AreEqual("System.Runtime.InteropServices.FieldOffsetAttribute", field.Attributes.Single().AttributeType.Resolve(ctx).FullName);
            arg = field.Attributes.Single().PositionalArguments.Single();
            Assert.AreEqual("System.Int32", arg.GetValueType(ctx).FullName);
            Assert.AreEqual(100, arg.GetValue(ctx));
        }
Пример #17
0
 public IncrementConstantValue(IConstantValue baseValue, int incrementAmount = 1)
 {
     if (baseValue == null)
     {
         throw new ArgumentNullException("baseValue");
     }
     if (baseValue is IncrementConstantValue icv)
     {
         this.baseValue       = icv.baseValue;
         this.incrementAmount = icv.incrementAmount + incrementAmount;
     }
     else
     {
         this.baseValue       = baseValue;
         this.incrementAmount = incrementAmount;
     }
 }
Пример #18
0
        internal override Expression Resolve(ParserContext parser)
        {
            HashSet <int>    intKeyDupeCheck    = new HashSet <int>();
            HashSet <string> stringKeyDupeCheck = new HashSet <string>();
            bool             dictHasStringKeys  = false;

            // Iterate through KVP in parallel so that errors will get reported in the preferred order.
            for (int i = 0; i < this.Keys.Length; ++i)
            {
                Expression     keyExpr      = this.Keys[i].Resolve(parser);
                IConstantValue keyConst     = keyExpr as IConstantValue;
                bool           keyIsString  = keyConst is StringConstant;
                bool           keyIsInteger = keyConst is IntegerConstant;
                if (!keyIsInteger && !keyIsString)
                {
                    throw new ParserException(keyExpr.FirstToken, "Only string and integer constants can be used as dictionary keys in inline definitions.");
                }

                if (i == 0)
                {
                    dictHasStringKeys = keyIsString;
                }
                else if (dictHasStringKeys != keyIsString)
                {
                    throw new ParserException(keyExpr.FirstToken, "Cannot mix types of dictionary keys.");
                }

                bool collision = false;
                if (keyIsString)
                {
                    string keyValue = ((StringConstant)keyConst).Value;
                    collision = stringKeyDupeCheck.Contains(keyValue);
                    stringKeyDupeCheck.Add(keyValue);
                }
                else
                {
                    int keyValue = ((IntegerConstant)keyConst).Value;
                    collision = intKeyDupeCheck.Contains(keyValue);
                    intKeyDupeCheck.Add(keyValue);
                }

                this.Keys[i]   = keyExpr;
                this.Values[i] = this.Values[i].Resolve(parser);
            }
            return(this);
        }
Пример #19
0
        private static bool TryImplicitConstantConversion(ref BoundExpression boundExpression, TypeSymbol targetType)
        {
            if (!boundExpression.IsConstant)
            {
                return(false);
            }
            if (boundExpression.ValueType == targetType)
            {
                return(false);
            }

            var targetSystemType = targetType.UdonType.SystemType;

            object constantValue = boundExpression.ConstantValue.Value;

            // if (targetSystemType == typeof(string))
            // {
            //     IConstantValue constant = new ConstantValue<string>(constantValue?.ToString() ?? "");
            //
            //     boundExpression = new BoundConstantExpression(constant, targetType, boundExpression.SyntaxNode);
            // }

            var sourceSystemType = boundExpression.ValueType.UdonType.SystemType;

            if (ConstantExpressionOptimizer.CanDoConstantConversion(sourceSystemType) &&
                ConstantExpressionOptimizer.CanDoConstantConversion(targetSystemType))
            {
                IConstantValue constant = (IConstantValue)Activator.CreateInstance(typeof(ConstantValue <>).MakeGenericType(targetSystemType), ConstantExpressionOptimizer.FoldConstantConversion(targetSystemType, constantValue));
                boundExpression = new BoundConstantExpression(constant, targetType, boundExpression.SyntaxNode);

                return(true);
            }

            if (boundExpression.ValueType.IsEnum &&
                boundExpression.ValueType.IsExtern &&
                UdonSharpUtils.IsIntegerType(targetSystemType))
            {
                boundExpression = new BoundConstantExpression(ConstantExpressionOptimizer.FoldConstantConversion(targetSystemType, constantValue), targetType);

                return(true);
            }

            return(false);
        }
Пример #20
0
        internal override Expression Resolve(ParserContext parser)
        {
            for (int i = 0; i < this.Args.Length; ++i)
            {
                this.Args[i] = this.Args[i].Resolve(parser);
            }

            this.Root = this.Root.Resolve(parser);

            // TODO: this is hardcoded just for Math.floor(numeric constant). Eventually, it'd be nice
            // for a few common functions to have a compile-time codepath here.
            // e.g. Core.parseInt, Math.sin, etc.
            if (this.Root is FunctionReference)
            {
                FunctionDefinition funcDef = ((FunctionReference)this.Root).FunctionDefinition;
                IConstantValue     cv      = this.SimplifyFunctionCall(funcDef, this.Args);
                if (cv != null)
                {
                    return((Expression)cv);
                }
            }

            if (this.Root is SpecialEntity)
            {
                if (this.Root is SpecialEntity.EnumMaxFunction)
                {
                    int max = ((SpecialEntity.EnumMaxFunction) this.Root).GetMax();
                    return(new IntegerConstant(this.Root.FirstToken, max, this.Owner));
                }

                if (this.Root is SpecialEntity.EnumValuesFunction)
                {
                    int[]             rawValues = ((SpecialEntity.EnumValuesFunction) this.Root).GetValues();
                    List <Expression> values    = new List <Expression>();
                    foreach (int rawValue in rawValues)
                    {
                        values.Add(new IntegerConstant(this.Root.FirstToken, rawValue, this.Owner));
                    }
                    return(new ListDefinition(this.FirstToken, values, AType.Integer(this.FirstToken), this.Owner, false, null));
                }
            }

            return(this);
        }
        public static BoundConstantExpression FoldConstantUnaryPrefixExpression(
            BindContext context,
            PrefixUnaryExpressionSyntax syntax,
            MethodSymbol unaryOperator,
            BoundExpression operand)
        {
            if (!operand.IsConstant)
            {
                return(null);
            }

            if (unaryOperator.IsOperator && unaryOperator.IsExtern)
            {
                object foldedValue;
                object operandValue = operand.ConstantValue.Value;

                switch (syntax.OperatorToken.Kind())
                {
                case SyntaxKind.MinusToken:
                    foldedValue = DynamicInvoke.UnaryNegate(operandValue);
                    break;

                case SyntaxKind.ExclamationToken:
                    foldedValue = DynamicInvoke.UnaryNot(operandValue);
                    break;

                case SyntaxKind.TildeToken:
                    foldedValue = DynamicInvoke.BitwiseNot(operandValue);
                    break;

                default:
                    return(null);
                }

                IConstantValue constantValue = (IConstantValue)Activator.CreateInstance(typeof(ConstantValue <>).MakeGenericType(foldedValue.GetType()), foldedValue);

                return(new BoundConstantExpression(constantValue, context.GetTypeSymbol(foldedValue.GetType()), syntax));
            }

            return(null);
        }
Пример #22
0
            public override Value EmitValue(EmitContext context)
            {
                Value returnValue = context.GetReturnValue(TargetExpression.ValueType);

                context.EmitValueAssignment(returnValue, TargetExpression);

                Type           targetType     = TargetExpression.ValueType.UdonType.SystemType;
                IConstantValue incrementValue = (IConstantValue)Activator.CreateInstance(
                    typeof(ConstantValue <>).MakeGenericType(targetType), Convert.ChangeType(1, targetType));
                BoundExpression expression = CreateBoundInvocation(context, null, InternalExpression.Method, null,
                                                                   new BoundExpression[] { BoundAccessExpression.BindAccess(returnValue), new BoundConstantExpression(incrementValue, TargetExpression.ValueType, SyntaxNode) });

                if (InternalExpression.Method.ReturnType != TargetExpression.ValueType)
                {
                    expression = new BoundCastExpression(null, expression, ValueType, true);
                }

                context.EmitSet(TargetExpression, expression);

                return(returnValue);
            }
		public UnresolvedSecurityDeclarationBlob(int securityAction, byte[] blob)
		{
			BlobReader reader = new BlobReader(blob, null);
			this.securityAction = new SimpleConstantValue(securityActionTypeReference, securityAction);
			this.blob = blob;
			if (reader.ReadByte() == '.') {
				// binary attribute
				uint attributeCount = reader.ReadCompressedUInt32();
				for (uint i = 0; i < attributeCount; i++) {
					unresolvedAttributes.Add(new UnresolvedSecurityAttribute(this, (int)i));
				}
			} else {
				// for backward compatibility with .NET 1.0: XML-encoded attribute
				var attr = new DefaultUnresolvedAttribute(permissionSetAttributeTypeReference);
				attr.ConstructorParameterTypes.Add(securityActionTypeReference);
				attr.PositionalArguments.Add(this.securityAction);
				string xml = System.Text.Encoding.Unicode.GetString(blob);
				attr.AddNamedPropertyArgument("XML", new SimpleConstantValue(KnownTypeReference.String, xml));
				unresolvedAttributes.Add(attr);
			}
		}
Пример #24
0
        public IMethod ResolveConstructor(ITypeResolveContext context)
        {
            CSharpResolver r    = new CSharpResolver(context);
            IType          type = attributeType.Resolve(context);
            int            totalArgumentCount = 0;

            if (positionalArguments != null)
            {
                totalArgumentCount += positionalArguments.Count;
            }
            if (namedCtorArguments != null)
            {
                totalArgumentCount += namedCtorArguments.Count;
            }
            ResolveResult[] arguments     = new ResolveResult[totalArgumentCount];
            string[]        argumentNames = new string[totalArgumentCount];
            int             i             = 0;

            if (positionalArguments != null)
            {
                while (i < positionalArguments.Count)
                {
                    IConstantValue cv = positionalArguments[i];
                    arguments[i] = new ConstantResolveResult(cv.GetValueType(context), cv.GetValue(context));
                    i++;
                }
            }
            if (namedCtorArguments != null)
            {
                foreach (var pair in namedCtorArguments)
                {
                    argumentNames[i] = pair.Key;
                    arguments[i]     = new ConstantResolveResult(pair.Value.GetValueType(context), pair.Value.GetValue(context));
                    i++;
                }
            }
            MemberResolveResult mrr = r.ResolveObjectCreation(type, arguments, argumentNames) as MemberResolveResult;

            return(mrr != null ? mrr.Member as IMethod : null);
        }
Пример #25
0
        public void ExplicitStructLayoutAttribute()
        {
            IAttribute attr = ctx.GetClass(typeof(ExplicitFieldLayoutStruct)).Attributes.Single();

            Assert.AreEqual("System.Runtime.InteropServices.StructLayoutAttribute", attr.AttributeType.Resolve(ctx).FullName);
            IConstantValue arg1 = attr.PositionalArguments.Single();

            Assert.AreEqual("System.Runtime.InteropServices.LayoutKind", arg1.GetValueType(ctx).FullName);
            Assert.AreEqual((int)LayoutKind.Explicit, arg1.GetValue(ctx));

            var arg2 = attr.NamedArguments[0];

            Assert.AreEqual("CharSet", arg2.Key);
            Assert.AreEqual("System.Runtime.InteropServices.CharSet", arg2.Value.GetValueType(ctx).FullName);
            Assert.AreEqual((int)CharSet.Unicode, arg2.Value.GetValue(ctx));

            var arg3 = attr.NamedArguments[1];

            Assert.AreEqual("Pack", arg3.Key);
            Assert.AreEqual("System.Int32", arg3.Value.GetValueType(ctx).FullName);
            Assert.AreEqual(8, arg3.Value.GetValue(ctx));
        }
Пример #26
0
 void ISupportsInterning.PrepareForInterning(IInterningProvider provider)
 {
     baseValue = provider.Intern(baseValue);
 }
Пример #27
0
 protected DefaultField(IField f) : base(f)
 {
     this.constantValue = f.ConstantValue;
     this.IsReadOnly    = f.IsReadOnly;
     this.IsVolatile    = f.IsVolatile;
 }
Пример #28
0
 public UnresolvedSecurityDeclaration(IConstantValue securityAction, byte[] blob)
 {
     Debug.Assert(securityAction != null);
     Debug.Assert(blob != null);
     this.securityAction = securityAction;
     this.blob = blob;
 }
Пример #29
0
		public void AddNamedArgument(string name, IConstantValue value)
		{
			CheckBeforeMutation();
			this.NamedArguments.Add(new KeyValuePair<string, IConstantValue>(name, value));
		}
		public void AddNamedFieldArgument(string fieldName, IConstantValue value)
		{
			this.NamedArguments.Add(new KeyValuePair<IMemberReference, IConstantValue>(
				new DefaultMemberReference(EntityType.Field, attributeType, fieldName),
				value
			));
		}
Пример #31
0
 public override void VisitConstantValueNode(IConstantValue constantValueParam, IHighlightingConsumer context)
 {
     context.AddHighlighting(new CgHighlighting(CgHighlightingAttributeIds.NUMBER, constantValueParam.GetDocumentRange()));
     base.VisitConstantValueNode(constantValueParam, context);
 }
			public ResolvedParameterWithDefaultValue(IConstantValue defaultValue, ITypeResolveContext context)
			{
				this.defaultValue = defaultValue;
				this.context = context;
			}
Пример #33
0
		void ISupportsInterning.PrepareForInterning(IInterningProvider provider)
		{
			baseValue = provider.Intern(baseValue);
		}
Пример #34
0
 public void AddNamedArgument(string name, IConstantValue value)
 {
     CheckBeforeMutation();
     this.NamedArguments.Add(new KeyValuePair <string, IConstantValue>(name, value));
 }
Пример #35
0
 public ResolvedParameterWithDefaultValue(IConstantValue defaultValue, ITypeResolveContext context)
 {
     this.defaultValue = defaultValue;
     this.context      = context;
 }
Пример #36
0
		static void AddNamedArgument(DefaultAttribute attribute, string name, IConstantValue value)
		{
			attribute.NamedArguments.Add(new KeyValuePair<string, IConstantValue>(name, value));
		}
		public void AddNamedPropertyArgument(string propertyName, IConstantValue value)
		{
			this.NamedArguments.Add(new KeyValuePair<IMemberReference, IConstantValue>(
				new DefaultMemberReference(EntityType.Property, attributeType, propertyName),
				value
			));
		}
Пример #38
0
 public BoundConstantExpression(IConstantValue constantValue, TypeSymbol constantType, SyntaxNode node)
     : base(node, null)
 {
     ConstantValue = constantValue;
     ConstantType  = constantType;
 }
Пример #39
0
		ResolveResult Resolve(IConstantValue constantValue, ITypeResolveContext context)
		{
			if (constantValue != null)
				return constantValue.Resolve(context);
			else
				return new ErrorResolveResult(SharedTypes.UnknownType);
		}
Пример #40
0
		protected DefaultField(IField f) : base(f)
		{
			this.constantValue = f.ConstantValue;
			this.IsReadOnly = f.IsReadOnly;
			this.IsVolatile = f.IsVolatile;
		}
Пример #41
0
		public override void ApplyInterningProvider(IInterningProvider provider)
		{
			base.ApplyInterningProvider(provider);
			if (provider != null)
				constantValue = provider.Intern(constantValue);
		}
Пример #42
0
		public IncrementConstantValue(IConstantValue baseValue, int incrementAmount = 1)
		{
			if (baseValue == null)
				throw new ArgumentNullException("baseValue");
			IncrementConstantValue icv = baseValue as IncrementConstantValue;
			if (icv != null) {
				this.baseValue = icv.baseValue;
				this.incrementAmount = icv.incrementAmount + incrementAmount;
			} else {
				this.baseValue = baseValue;
				this.incrementAmount = incrementAmount;
			}
		}
Пример #43
0
		public Expression ConvertConstantValue(IConstantValue constantValue)
		{
			if (constantValue == null)
				throw new ArgumentNullException("constantValue");
			return ConvertConstantValue(constantValue.Resolve(context));
		}
Пример #44
0
		void ISupportsInterning.PrepareForInterning(IInterningProvider provider)
		{
			name = provider.Intern(name);
			type = provider.Intern(type);
			attributes = provider.InternList(attributes);
			defaultValue = provider.Intern(defaultValue);
		}
        public static BoundConstantExpression FoldConstantBinaryExpression(
            BindContext context,
            BinaryExpressionSyntax syntax,
            MethodSymbol binaryOperator,
            BoundExpression lhs, BoundExpression rhs)
        {
            // Value type + null comparison which will always be false for == and true for !=
            // This folding is needed for null comparisons in generics to work as expected
            if ((lhs.ValueType.IsValueType && rhs.IsConstant && rhs.ConstantValue.Value == null) ||
                (rhs.ValueType.IsValueType && lhs.IsConstant && lhs.ConstantValue.Value == null))
            {
                if (syntax.OperatorToken.Kind() == SyntaxKind.EqualsEqualsToken)
                {
                    return(new BoundConstantExpression(new ConstantValue <bool>(false),
                                                       context.GetTypeSymbol(typeof(bool)), syntax));
                }

                if (syntax.OperatorToken.Kind() == SyntaxKind.ExclamationEqualsToken)
                {
                    return(new BoundConstantExpression(new ConstantValue <bool>(true),
                                                       context.GetTypeSymbol(typeof(bool)), syntax));
                }
            }

            if (!lhs.IsConstant || !rhs.IsConstant)
            {
                return(null);
            }

            if (binaryOperator == null || (binaryOperator.IsOperator && binaryOperator.IsExtern))
            {
                object foldedValue;
                object lhsValue = lhs.ConstantValue.Value;
                object rhsValue = rhs.ConstantValue.Value;

                switch (syntax.OperatorToken.Kind())
                {
                case SyntaxKind.PlusToken:
                    foldedValue = DynamicInvoke.Add(lhsValue, rhsValue);
                    break;

                case SyntaxKind.MinusToken:
                    foldedValue = DynamicInvoke.Sub(lhsValue, rhsValue);
                    break;

                case SyntaxKind.AsteriskToken:
                    foldedValue = DynamicInvoke.Mul(lhsValue, rhsValue);
                    break;

                case SyntaxKind.SlashToken:
                    foldedValue = DynamicInvoke.Div(lhsValue, rhsValue);
                    break;

                case SyntaxKind.PercentToken:
                    foldedValue = DynamicInvoke.Mod(lhsValue, rhsValue);
                    break;

                case SyntaxKind.LessThanLessThanToken:
                    foldedValue = DynamicInvoke.LSh(lhsValue, rhsValue);
                    break;

                case SyntaxKind.GreaterThanGreaterThanToken:
                    foldedValue = DynamicInvoke.RSh(lhsValue, rhsValue);
                    break;

                case SyntaxKind.CaretToken:
                    foldedValue = DynamicInvoke.Xor(lhsValue, rhsValue);
                    break;

                case SyntaxKind.AmpersandToken:
                case SyntaxKind.AmpersandAmpersandToken:     // When we're dealing with constants short circuiting shouldn't matter
                    foldedValue = DynamicInvoke.BitwiseAnd(lhsValue, rhsValue);
                    break;

                case SyntaxKind.BarToken:
                case SyntaxKind.BarBarToken:
                    foldedValue = DynamicInvoke.BitwiseOr(lhsValue, rhsValue);
                    break;

                case SyntaxKind.GreaterThanToken:
                    foldedValue = DynamicInvoke.GreaterThan(lhsValue, rhsValue);
                    break;

                case SyntaxKind.GreaterThanEqualsToken:
                    foldedValue = DynamicInvoke.GreaterThanOrEquals(lhsValue, rhsValue);
                    break;

                case SyntaxKind.LessThanToken:
                    foldedValue = DynamicInvoke.LessThan(lhsValue, rhsValue);
                    break;

                case SyntaxKind.LessThanOrEqualExpression:
                    foldedValue = DynamicInvoke.LessThanOrEquals(lhsValue, rhsValue);
                    break;

                case SyntaxKind.EqualsEqualsToken:
                    foldedValue = DynamicInvoke.Equal(lhsValue, rhsValue);
                    break;

                case SyntaxKind.ExclamationEqualsToken:
                    foldedValue = DynamicInvoke.NotEqual(lhsValue, rhsValue);
                    break;

                default:
                    return(null);
                }

                IConstantValue constantValue = (IConstantValue)Activator.CreateInstance(typeof(ConstantValue <>).MakeGenericType(foldedValue.GetType()), foldedValue);

                return(new BoundConstantExpression(constantValue, context.GetTypeSymbol(foldedValue.GetType()), syntax));
            }

            return(null);
        }