Example #1
0
		public override void PrintCode (CodeWriter cp)
		{
			if (tryBlock == null) return;
			
			cp.Write ("try {");
			cp.Indent ();
			condition.PrintCode (cp);
			cp.Unindent ();
			foreach (DictionaryEntry de in catchBlocks) {
				CodeVariableDeclaration vd = (CodeVariableDeclaration) de.Key;
				cp.Write ("} catch (");
				if (vd.Variable.Name.Length > 0)
					vd.PrintCode (cp);
				else
					cp.Write (vd.Variable.Type.FullName);
				cp.Write (") {");
				cp.Indent ();
				((CodeBlock) de.Value).PrintCode (cp);
				cp.Unindent ();
			}
			if (!finallyBlock.IsEmpty) {
				cp.Write ("} finally {");
				cp.Indent ();
				finallyBlock.PrintCode (cp);
				cp.Unindent ();
			}
			cp.Write ("}");
		}
Example #2
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");
		}
Example #3
0
        public CodeVariableReference DeclareVariable(Type type, CodeExpression initValue)
        {
            string name = "v" + (varId++);
            CodeVariableDeclaration var = new CodeVariableDeclaration(type, name);

            currentBlock.Add(var);
            if (!object.ReferenceEquals(initValue, null))
            {
                Assign(var.Variable, initValue);
            }
            return(var.Variable);
        }
Example #4
0
        public override void Generate(ILGenerator gen)
        {
            Type t = array.GetResultType();

            if (t.IsArray)
            {
                CodeBlock block = new CodeBlock();
                CodeVariableDeclaration indexDec;
                CodeWhile          cw;
                CodeValueReference index;
                CodeValueReference item;

                block.Add(itemDec);
                item = itemDec.Variable;
                block.Add(indexDec = new CodeVariableDeclaration(typeof(int), "n"));
                index = indexDec.Variable;
                block.Add(new CodeAssignment(index, new CodeLiteral(0)));

                block.Add(cw = new CodeWhile(CodeExpression.IsSmallerThan(index, array.ArrayLength)));
                CodeBlock loopBlock = new CodeBlock();
                loopBlock.Add(new CodeAssignment(item, array[index]));
                loopBlock.Add(new CodeIncrement(index));
                loopBlock.Add(forBlock);
                cw.WhileBlock = loopBlock;

                block.Generate(gen);
            }
            else
            {
                CodeBlock block = new CodeBlock();
                CodeVariableDeclaration dec;
                CodeWhile          cw;
                CodeValueReference enumerator;
                CodeValueReference item;

                block.Add(itemDec);
                item          = itemDec.Variable;
                block.Add(dec = new CodeVariableDeclaration(typeof(IEnumerator), "e"));
                enumerator    = dec.Variable;
                block.Add(new CodeAssignment(enumerator, array.Call("GetEnumerator")));

                block.Add(cw = new CodeWhile(enumerator.Call("MoveNext")));
                CodeBlock loopBlock = new CodeBlock();
                loopBlock.Add(new CodeAssignment(item, enumerator["Current"]));
                loopBlock.Add(forBlock);
                cw.WhileBlock = loopBlock;

                block.Generate(gen);
            }
        }
Example #5
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");
        }
Example #6
0
		public override void Generate (ILGenerator gen)
		{
			Type t = array.GetResultType ();
			if (t.IsArray)
			{
				CodeBlock block = new CodeBlock();
				CodeVariableDeclaration indexDec;
				CodeWhile cw;
				CodeValueReference index;
				CodeValueReference item;
				
				block.Add (itemDec);
				item = itemDec.Variable;
				block.Add (indexDec = new CodeVariableDeclaration (typeof(int), "n"));
				index = indexDec.Variable;
				block.Add (new CodeAssignment (index, new CodeLiteral (0)));
				
				block.Add (cw = new CodeWhile (CodeExpression.IsSmallerThan (index, array.ArrayLength)));
				CodeBlock loopBlock = new CodeBlock ();
				loopBlock.Add (new CodeAssignment (item, array[index]));
				loopBlock.Add (new CodeIncrement(index));
				loopBlock.Add (forBlock);
				cw.WhileBlock = loopBlock;
				
				block.Generate (gen);
			}
			else
			{
				CodeBlock block = new CodeBlock();
				CodeVariableDeclaration dec;
				CodeWhile cw;
				CodeValueReference enumerator;
				CodeValueReference item;
				
				block.Add (itemDec);
				item = itemDec.Variable;
				block.Add (dec = new CodeVariableDeclaration (typeof(IEnumerator), "e"));
				enumerator = dec.Variable;
				block.Add (new CodeAssignment (enumerator, array.Call("GetEnumerator")));
				
				block.Add (cw = new CodeWhile (enumerator.Call ("MoveNext")));
				CodeBlock loopBlock = new CodeBlock ();
				loopBlock.Add (new CodeAssignment (item, enumerator["Current"]));
				loopBlock.Add (forBlock);
				cw.WhileBlock = loopBlock;
				
				block.Generate (gen);
			}
		}
Example #7
0
		public override void Generate (ILGenerator gen)
		{
			gen.BeginExceptionBlock ();
			tryBlock.Generate (gen);
			foreach (DictionaryEntry de in catchBlocks) {
				CodeVariableDeclaration vd = (CodeVariableDeclaration) de.Key;
				gen.BeginCatchBlock (vd.Variable.Type);
				if (vd.Variable.Name.Length > 0) {
					vd.Generate (gen);
					// FIXME: assign exception to this local declaration
				}
				((CodeBlock) de.Value).Generate (gen);
			}
			if (!finallyBlock.IsEmpty) {
				gen.BeginFinallyBlock ();
				finallyBlock.Generate (gen);
			}
			gen.EndExceptionBlock ();
		}
Example #8
0
		static void GenerateEndMethodImpl (CodeClass c, MethodInfo endProcessMethod, string name, MethodInfo mi)
		{
			CodeMethod m = c.ImplementMethod (mi);
			CodeBuilder b = m.CodeBuilder;
			ParameterInfo [] pinfos = mi.GetParameters ();

			ParameterInfo p = pinfos [0];
			CodeArgumentReference asyncResultRef = m.GetArg (0);
			
			CodeVariableDeclaration paramsDecl = new CodeVariableDeclaration (typeof (object []), "parameters");
			b.CurrentBlock.Add (paramsDecl);
			CodeVariableReference paramsRef = paramsDecl.Variable;
			b.Assign (paramsRef,
				  new CodeNewArray (typeof (object), new CodeLiteral (pinfos.Length - 1)));
			/*
			for (int i = 0; i < pinfos.Length - 2; i++) {
				ParameterInfo par = pinfos [i];
				if (!par.IsOut)
					b.Assign (
						new CodeArrayItem (paramsRef, new CodeLiteral (i)),
						new CodeCast (typeof (object),
							new CodeArgumentReference (par.ParameterType, par.Position + 1, "arg" + i)));
			}
			*/
#if USE_OD_REFERENCE_IN_PROXY
			CodePropertyReference argMethodInfo = GetOperationMethod (m, b, name, "EndMethod");
#else
			CodeMethodCall argMethodInfo = new CodeMethodCall (typeof (MethodBase), "GetCurrentMethod");
#endif
			CodeLiteral argOperName = new CodeLiteral (name);
			
			CodeVariableReference retValue = null;
			if (mi.ReturnType == typeof (void))
				b.Call (m.GetThis (), endProcessMethod, argMethodInfo, argOperName, paramsRef, asyncResultRef);
			else {
				CodeVariableDeclaration retValueDecl = new CodeVariableDeclaration (mi.ReturnType, "retValue");
				b.CurrentBlock.Add (retValueDecl);
				retValue = retValueDecl.Variable;
				b.Assign (retValue,
					new CodeCast (mi.ReturnType,
						b.CallFunc (m.GetThis (), endProcessMethod, argMethodInfo, argOperName, paramsRef, asyncResultRef)));
			}
			// FIXME: fill out parameters
			if (retValue != null)
				b.Return (retValue);
		}
Example #9
0
		static void GenerateBeginMethodImpl (CodeClass c, MethodInfo beginProcessMethod, string name, MethodInfo mi)
		{
			CodeMethod m = c.ImplementMethod (mi);
			CodeBuilder b = m.CodeBuilder;
			// object [] parameters = new object [x];
			// parameters [0] = arg1;
			// parameters [1] = arg2;
			// ...
			// (return) BeginProcess (Contract.Operations [operName].BeginMethod, operName, parameters, asyncCallback, userState);
			ParameterInfo [] pinfos = mi.GetParameters ();
			CodeVariableDeclaration paramsDecl = new CodeVariableDeclaration (typeof (object []), "parameters");
			b.CurrentBlock.Add (paramsDecl);
			CodeVariableReference paramsRef = paramsDecl.Variable;
			b.Assign (paramsRef,
				  new CodeNewArray (typeof (object), new CodeLiteral (pinfos.Length - 2)));
			for (int i = 0; i < pinfos.Length - 2; i++) {
				ParameterInfo par = pinfos [i];
				if (!par.IsOut)
					b.Assign (
						new CodeArrayItem (paramsRef, new CodeLiteral (i)),
						new CodeCast (typeof (object), m.GetArg (i)));
			}
#if USE_OD_REFERENCE_IN_PROXY
			CodePropertyReference argMethodInfo = GetOperationMethod (m, b, name, "BeginMethod");
#else
			CodeMethodCall argMethodInfo = new CodeMethodCall (typeof (MethodBase), "GetCurrentMethod");
#endif
			CodeLiteral argOperName = new CodeLiteral (name);

			ParameterInfo p = pinfos [pinfos.Length - 2];
			CodeArgumentReference callbackRef = new CodeArgumentReference (typeof (AsyncCallback), p.Position + 1, p.Name);
			p = pinfos [pinfos.Length - 1];
			CodeArgumentReference stateRef = new CodeArgumentReference (typeof (object), p.Position + 1, p.Name);

			CodeVariableDeclaration retValueDecl = new CodeVariableDeclaration (mi.ReturnType, "retValue");
			b.CurrentBlock.Add (retValueDecl);
			CodeVariableReference retValue = retValueDecl.Variable;
			b.Assign (retValue,
				new CodeCast (mi.ReturnType,
					b.CallFunc (m.GetThis (), beginProcessMethod, argMethodInfo, argOperName, paramsRef, callbackRef, stateRef)));

			b.Return (retValue);
		}
Example #10
0
		static void GenerateMethodImpl (CodeClass c, MethodInfo processMethod, string name, MethodInfo mi)
		{
			CodeMethod m = c.ImplementMethod (mi);
			CodeBuilder b = m.CodeBuilder;
			// object [] parameters = new object [x];
			// parameters [0] = arg1;
			// parameters [1] = arg2;
			// ...
			// (return) Process (Contract.Operations [operName].SyncMethod, operName, parameters);
			ParameterInfo [] pinfos = mi.GetParameters ();
			CodeVariableDeclaration paramsDecl = new CodeVariableDeclaration (typeof (object []), "parameters");
			b.CurrentBlock.Add (paramsDecl);
			CodeVariableReference paramsRef = paramsDecl.Variable;
			b.Assign (paramsRef,
				  new CodeNewArray (typeof (object), new CodeLiteral (pinfos.Length)));
			for (int i = 0; i < pinfos.Length; i++) {
				ParameterInfo par = pinfos [i];
				if (!par.IsOut)
					b.Assign (
						new CodeArrayItem (paramsRef, new CodeLiteral (i)),
						new CodeCast (typeof (object),
							new CodeArgumentReference (par.ParameterType, par.Position + 1, "arg" + i)));
			}
#if USE_OD_REFERENCE_IN_PROXY
			CodePropertyReference argMethodInfo = GetOperationMethod (m, b, name, "SyncMethod");
#else
			CodeMethodCall argMethodInfo = new CodeMethodCall (typeof (MethodBase), "GetCurrentMethod");
#endif
			CodeLiteral argOperName = new CodeLiteral (name);
			CodeVariableReference retValue = null;
			if (mi.ReturnType == typeof (void))
				b.Call (m.GetThis (), processMethod, argMethodInfo, argOperName, paramsRef);
			else {
				CodeVariableDeclaration retValueDecl = new CodeVariableDeclaration (mi.ReturnType, "retValue");
				b.CurrentBlock.Add (retValueDecl);
				retValue = retValueDecl.Variable;
				b.Assign (retValue,
					new CodeCast (mi.ReturnType,
						b.CallFunc (m.GetThis (), processMethod, argMethodInfo, argOperName, paramsRef)));
			}
			for (int i = 0; i < pinfos.Length; i++) {
				ParameterInfo par = pinfos [i];
				if (par.IsOut || par.ParameterType.IsByRef)
					b.Assign (
						new CodeArgumentReference (par.ParameterType, par.Position + 1, "arg" + i),
						new CodeCast (par.ParameterType.GetElementType (),
							new CodeArrayItem (paramsRef, new CodeLiteral (i))));
			}
			if (retValue != null)
				b.Return (retValue);
		}
		public CodeVariableReference DeclareVariable (Type type, CodeExpression initValue)
		{
			string name = "v" + (varId++);
			CodeVariableDeclaration var = new CodeVariableDeclaration (type, name);
			currentBlock.Add (var);
			if (!object.ReferenceEquals (initValue, null)) 
				Assign (var.Variable, initValue);
			return var.Variable;
		}