Esempio n. 1
0
		Type[] GetParameterTypes (CodeExpression[] parameters)
		{
			Type[] ts = new Type [parameters.Length];
			for (int n=0; n<ts.Length; n++)
				ts [n] = parameters[n].GetResultType ();
			return ts;
		}
Esempio n. 2
0
		public CodeFieldReference (CodeExpression target, FieldInfo field)
		{
			if (field.IsStatic)
				throw new InvalidOperationException ("Static member '" + field.Name + "' cannot be accessed with an instance reference.");
			this.target = target;
			this.field = field;		
		}
Esempio n. 3
0
		public CodeNewArray (Type type, CodeExpression size)
		{
			this.elemType = type;
			this.size = size;
			if (size.GetResultType () != typeof(int))
				throw new InvalidOperationException ("Array size must be an Int32");
		}
Esempio n. 4
0
		public CodeFor (CodeExpression initExp, CodeExpression conditionExp, CodeExpression nextExp)
		{
			this.initExp = initExp;	
			this.conditionExp = conditionExp;
			this.nextExp = nextExp;
			
			if (conditionExp.GetResultType () != typeof(bool))
				throw new InvalidOperationException ("Condition expression is not boolean"); 
		}
		public CodeArrayItem (CodeExpression array, CodeExpression index)
		{
			if (array == null)
				throw new ArgumentNullException ("array");
			if (index == null)
				throw new ArgumentNullException ("index");
			this.array = array;
			this.index = index;		
		}
Esempio n. 6
0
		public CodeSubstractOne (CodeExpression exp)
		{
			this.exp = exp;
			if (!exp.IsNumber) {
				decMet = exp.GetResultType ().GetMethod ("op_Decrement");
				if (decMet == null)
					throw new InvalidOperationException ("Operator '--' cannot be applied to operand of type '" + exp.GetResultType().FullName + "'");
			}
		}
 public CodeAssignment(CodeValueReference var, CodeExpression exp)
 {
     if (var == null)
         throw new ArgumentNullException ("var");
     if (exp == null)
         throw new ArgumentNullException ("exp");
     this.exp = exp;
     this.var = var;
 }
Esempio n. 8
0
		public CodeWhen (CodeExpression condition, CodeExpression trueResult, CodeExpression falseResult)
		{
			this.condition = condition;
			if (condition.GetResultType () != typeof(bool))
				throw new InvalidOperationException ("Condition expression is not boolean");
			if (trueResult.GetResultType() != falseResult.GetResultType())
				throw new InvalidOperationException ("The types of the true and false expressions must be the same");
			trueBlock = trueResult;
			falseBlock = falseResult;
		}
Esempio n. 9
0
		public CodeMethodCall (CodeExpression target, string name, params CodeExpression[] parameters)
		{
			this.target = target;
			this.parameters = parameters;
			Type[] types = GetParameterTypes (parameters);
			method = target.GetResultType().GetMethod (name, types);
			if (method == null) {
				throw new InvalidOperationException ("Method " + GetSignature(target.GetResultType(), name, parameters) + " not found");
			}
		}
Esempio n. 10
0
		public CodeAnd (CodeExpression exp1, CodeExpression exp2)
		{
			this.exp1 = exp1;
			this.exp2 = exp2;
			
 			if (exp1.GetResultType () != typeof(bool) || exp1.GetResultType () != typeof(bool)) {
				if (t1 != t2)
					throw new InvalidOperationException ("Can't compare values of different primitive types");
			}
		}
Esempio n. 11
0
		public CodeForeach (CodeExpression array, Type itemType)
		{
			this.array = array;
			this.itemType = itemType;
			
			Type t = array.GetResultType ();
			if (!t.IsArray && t.GetMethod ("GetEnumerator", Type.EmptyTypes) == null)
				throw new InvalidOperationException ("foreach statement cannot operate on variables of type `" + t + "' because that class does not provide a GetEnumerator method or it is inaccessible");
			
			itemDec = new CodeVariableDeclaration (itemType, "item");
		}
Esempio n. 12
0
		public static void GenerateMethodCall (ILGenerator gen, CodeExpression target, MethodBase method, params CodeExpression[] parameters)
		{
			Type[] ptypes = Type.EmptyTypes;
			// It could raise an error since GetParameters() on MethodBuilder is not supported.
			if (parameters.Length > 0) {
				ParameterInfo[] pars = method.GetParameters ();
				ptypes = new Type[pars.Length];
				for (int n=0; n<ptypes.Length; n++) ptypes[n] = pars[n].ParameterType;
			}
			GenerateMethodCall (gen, target, method, ptypes, parameters);
		}
Esempio n. 13
0
		public CodeNotEquals (CodeExpression exp1, CodeExpression exp2)
		{
			this.exp1 = exp1;
			this.exp2 = exp2;
			
			t1 = exp1.GetResultType ();
			t2 = exp2.GetResultType ();
			
			if (t1.IsValueType && t2.IsValueType) {
				if (t1 != t2)
					throw new InvalidOperationException ("Can't compare values of different primitive types");
			}
		}
Esempio n. 14
0
		public CodeAdd (CodeExpression exp1, CodeExpression exp2)
		{
			this.symbol = "+";
			this.exp1 = exp1;
			this.exp2 = exp2;
			
			t1 = exp1.GetResultType ();
			t2 = exp2.GetResultType ();
			
			if ((!t1.IsPrimitive || !t2.IsPrimitive || (t1 != t2)) && (t1 != typeof(string) || t2 != typeof(string))) {
				throw new InvalidOperationException ("Operator " + GetType().Name + " cannot be applied to operands of type '" + t1.Name + " and " + t2.Name);
			}
		}
        public CodeBinaryOperation(CodeExpression exp1, CodeExpression exp2, string symbol)
        {
            this.symbol = symbol;
            this.exp1 = exp1;
            this.exp2 = exp2;

            t1 = exp1.GetResultType ();
            t2 = exp2.GetResultType ();

            if (!t1.IsPrimitive || !t2.IsPrimitive || (t1 != t2)) {
                throw new InvalidOperationException ("Operator " + GetType().Name + " cannot be applied to operands of type '" + t1.Name + " and " + t2.Name);
            }
        }
Esempio n. 16
0
		public override void GenerateSet (ILGenerator gen, CodeExpression value)
		{
			if (field.IsStatic) {
				value.Generate (gen);
				CodeGenerationHelper.GenerateSafeConversion (gen, field.FieldType, value.GetResultType ());
				gen.Emit (OpCodes.Stsfld, field);
			}
			else {
				target.Generate (gen);
				value.Generate (gen);
				CodeGenerationHelper.GenerateSafeConversion (gen, field.FieldType, value.GetResultType ());
				gen.Emit (OpCodes.Stfld, field);
			}
		}
 public override void GenerateSet(ILGenerator gen, CodeExpression value)
 {
     if (type.IsByRef) {
         gen.Emit (OpCodes.Ldarg, argNum);
         value.Generate (gen);
         CodeGenerationHelper.GenerateSafeConversion (gen, type.GetElementType (), value.GetResultType ());
         CodeGenerationHelper.SaveToPtr (gen, type.GetElementType ());
     }
     else {
         value.Generate (gen);
         CodeGenerationHelper.GenerateSafeConversion (gen, type, value.GetResultType ());
         gen.Emit (OpCodes.Starg, argNum);
     }
 }
Esempio n. 18
0
		static void GenerateMethodCall (ILGenerator gen, CodeExpression target, MethodBase method, Type[] parameterTypes, params CodeExpression[] parameters)
		{
			OpCode callOp;
			
			if (parameterTypes.Length != parameters.Length)
				throw GetMethodException (method, "Invalid number of parameters, expected " + parameterTypes.Length + ", found " + parameters.Length + ".");  
			
			if (!object.ReferenceEquals (target, null)) 
			{
				target.Generate (gen);
				
				Type targetType = target.GetResultType();
				if (targetType.IsValueType) {
					LocalBuilder lb = gen.DeclareLocal (targetType);
					gen.Emit (OpCodes.Stloc, lb);
					gen.Emit (OpCodes.Ldloca, lb);
					callOp = OpCodes.Call;
				}
				else
					callOp = OpCodes.Callvirt;
			}
			else
				callOp = OpCodes.Call;

			for (int n=0; n<parameterTypes.Length; n++) {
				try {
					CodeExpression par = parameters[n];
					par.Generate (gen);
					GenerateSafeConversion (gen, parameterTypes[n], par.GetResultType());
				}
				catch (InvalidOperationException ex) {
					throw GetMethodException (method, "Parameter " + n + ". " + ex.Message);  
				}
			}
			
			if (method is MethodInfo)
				gen.Emit (callOp, (MethodInfo)method);
			else if (method is ConstructorInfo)
				gen.Emit (callOp, (ConstructorInfo)method);
		}
Esempio n. 19
0
 public void Assign(CodeValueReference var, CodeExpression val)
 {
     currentBlock.Add(new CodeAssignment(var, val));
 }
		public static CodeExpression Multiply (CodeExpression e1, CodeExpression e2)
		{
			return new CodeMul (e1, e2);
		}
		public static CodeExpression Divide (CodeExpression e1, CodeExpression e2)
		{
			return new CodeDiv (e1, e2);
		}
Esempio n. 22
0
 public void While(CodeExpression condition)
 {
     currentBlock.Add(new CodeWhile(condition));
     PushNewBlock();
 }
		public static CodeExpression Subtract (CodeExpression e1, CodeExpression e2)
		{
			return new CodeSub (e1, e2);
		}
Esempio n. 24
0
 public void For(CodeExpression initExp, CodeExpression conditionExp, CodeExpression nextExp)
 {
     currentBlock.Add(new CodeFor(initExp, conditionExp, nextExp));
     PushNewBlock();
 }
Esempio n. 25
0
 internal CodeReturn(CodeBuilder codeBuilder, CodeExpression retValue)
 {
     this.codeBuilder = codeBuilder;
     this.retValue    = retValue;
 }
Esempio n. 26
0
 public CodeExpression When(CodeExpression condition, CodeExpression trueResult, CodeExpression falseResult)
 {
     return(new CodeWhen(condition, trueResult, falseResult));
 }
Esempio n. 27
0
        public override void GenerateSet(ILGenerator gen, CodeExpression value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            Type t = array.GetResultType().GetElementType();

            if (t.IsEnum && t != typeof(Enum))
            {
                t = t.UnderlyingSystemType;
            }

            array.Generate(gen);
            index.Generate(gen);

            switch (Type.GetTypeCode(t))
            {
            case TypeCode.Byte:
            case TypeCode.SByte:
                value.Generate(gen);
                gen.Emit(OpCodes.Stelem_I1);
                break;

            case TypeCode.Double:
                value.Generate(gen);
                gen.Emit(OpCodes.Stelem_R8);
                break;

            case TypeCode.UInt16:
            case TypeCode.Int16:
                value.Generate(gen);
                gen.Emit(OpCodes.Stelem_I2);
                break;

            case TypeCode.UInt32:
            case TypeCode.Int32:
                value.Generate(gen);
                gen.Emit(OpCodes.Stelem_I4);
                break;

            case TypeCode.UInt64:
            case TypeCode.Int64:
                value.Generate(gen);
                gen.Emit(OpCodes.Stelem_I8);
                break;

            case TypeCode.Single:
                value.Generate(gen);
                gen.Emit(OpCodes.Stelem_R4);
                break;

            default:
                if (t.IsValueType)
                {
                    gen.Emit(OpCodes.Ldelema, t);
                    value.Generate(gen);
                    gen.Emit(OpCodes.Stobj, t);
                }
                else
                {
                    value.Generate(gen);
                    gen.Emit(OpCodes.Stelem_Ref);
                }
                break;
            }
        }
Esempio n. 28
0
File: Exp.cs Progetto: nickchal/pash
		public static CodeExpression And (CodeExpression e1, CodeExpression e2, CodeExpression e3)
		{
			return new CodeAnd (new CodeAnd (e1, e2), e3);
		} 
 public override void GenerateSet(ILGenerator gen, CodeExpression value)
 {
     value.Generate(gen);
     CodeGenerationHelper.GenerateSafeConversion(gen, type, value.GetResultType());
     gen.Emit(OpCodes.Stloc, localBuilder);
 }
Esempio n. 30
0
 public void Return(CodeExpression exp)
 {
     currentBlock.Add(new CodeReturn(this, exp));
 }
Esempio n. 31
0
 public void If(CodeExpression condition)
 {
     currentBlock.Add(new CodeIf(condition));
     PushNewBlock();
     nestedIfs.Add(0);
 }
 public abstract void GenerateSet(ILGenerator gen, CodeExpression value);
Esempio n. 33
0
 public abstract void GenerateSet(ILGenerator gen, CodeExpression value);
Esempio n. 34
0
 public CodeVariableReference DeclareVariable(CodeExpression initValue)
 {
     return(DeclareVariable(initValue.GetResultType(), initValue));
 }
Esempio n. 35
0
		internal CodeReturn (CodeBuilder codeBuilder, CodeExpression retValue)
		{
			this.codeBuilder = codeBuilder;
			this.retValue = retValue;
		}
Esempio n. 36
0
        public CodeFieldReference DefineField(string name, Type type, FieldAttributes attrs, CodeExpression initialValue, params CodeCustomAttribute [] customAttributes)
        {
            FieldBuilder fb = typeBuilder.DefineField(GetFieldName(name), type, attrs);

            foreach (CodeCustomAttribute a in customAttributes)
            {
                fb.SetCustomAttribute(a.Builder);
            }
            fieldAttributes [fb] = new ArrayList(customAttributes);
            fields.Add(fb);
            CodeFieldReference fr;

            if ((attrs & FieldAttributes.Static) != 0)
            {
                fr = new CodeFieldReference(fb);
            }
            else
            {
                fr = new CodeFieldReference(new CodeArgumentReference(TypeBuilder, 0, "this"), fb);
            }

            if (null != (object)initialValue)
            {
                CodeBuilder cb = (attrs & FieldAttributes.Static) == 0 ? GetInstanceInitBuilder() : GetClassInitBuilder();
                cb.Assign(fr, initialValue);
            }
            return(fr);
        }
		public override void GenerateSet (ILGenerator gen, CodeExpression value)
		{
			exp.GenerateSet (gen, value);
		}
Esempio n. 38
0
File: Exp.cs Progetto: nickchal/pash
		public static CodeExpression Call (CodeExpression target, string name, params CodeExpression[] parameters)
		{
			return new CodeMethodCall (target, name, parameters);
		}
Esempio n. 39
0
 public CodeIs(Type type, CodeExpression exp)
 {
     this.type = type;
     this.exp = exp;
 }
Esempio n. 40
0
 public CodeFieldReference DefineStaticField(CodeExpression initialValue, params CodeCustomAttribute [] customAttributes)
 {
     return(DefineField(GetFieldName(null), initialValue.GetResultType(), FieldAttributes.Public | FieldAttributes.Static, initialValue, customAttributes));
 }
Esempio n. 41
0
File: Exp.cs Progetto: nickchal/pash
		public static CodeExpression NewArray (Type type, CodeExpression size)
		{
			return new CodeNewArray (type, size);
		}
Esempio n. 42
0
 public CodeFieldReference DefineStaticField(string name, Type type, CodeExpression initialValue, params CodeCustomAttribute [] customAttributes)
 {
     return(DefineField(GetFieldName(name), type, FieldAttributes.Public | FieldAttributes.Static, initialValue, customAttributes));
 }
Esempio n. 43
0
File: Exp.cs Progetto: nickchal/pash
		public static CodeExpression Or (CodeExpression e1, CodeExpression e2)
		{
			return new CodeOr (e1, e2);
		}
Esempio n. 44
0
 public CodeMethodCall(CodeExpression target, CodeMethod method, params CodeExpression[] parameters)
 {
     this.target     = target;
     this.parameters = parameters;
     this.codeMethod = method;
 }
Esempio n. 45
0
File: Exp.cs Progetto: nickchal/pash
		public static CodeExpression Call (CodeExpression target, CodeMethod method, params CodeExpression[] parameters)
		{
			return new CodeMethodCall (target, method, parameters);
		}
Esempio n. 46
0
 public override void GenerateSet(ILGenerator gen, CodeExpression value)
 {
     exp.GenerateSet(gen, value);
 }