Example #1
0
		public FunctionData CloneAsResolvedNativeType (bool as32bit)
		{
			var rv = new FunctionData ();
			rv.Comment = Comment + " cloned for " + (as32bit ? "32bit" : "64bit");
			rv.Prefix = Prefix;
			rv.Variants = Variants;
			if (ReturnType != null)
				rv.ReturnType = ReturnType.AsSpecificNativeType (as32bit);
			if (Parameters != null) {
				rv.Parameters = new ParameterData [Parameters.Length];
				for (int i = 0; i < Parameters.Length; i++) {
					rv.Parameters [i] = new ParameterData ();
					rv.Parameters [i].TypeData = Parameters [i].TypeData.AsSpecificNativeType (as32bit);
					rv.Parameters [i].IsRef = Parameters [i].IsRef;
				}
			}
			rv.MarshalExceptions = MarshalExceptions;
			return rv;
		}
Example #2
0
		static void Write_objc_msgSend (StringWriter writer, FunctionData func)
		{
			// func name
			var overload = "objc_msgSend";
			var funcName = GetFuncName (func, overload);

			// typedef
			writer.Write ("typedef {0} (*func_{1}) (id self, SEL sel", func.ReturnType == null ? "void" : func.ReturnType.NativeType, funcName.ToString ());
			if (func.Parameters != null) {
				for (int i = 0; i < func.Parameters.Length; i++) {
					writer.Write (", {0} f{1}", func.Parameters [i].TypeData.NativeType, i);
				}
			}
			writer.WriteLine (");");

			// declaration
			writer.WriteLine (func.ReturnType != null ? func.ReturnType.NativeWrapperType : "void");
			writer.Write (funcName);
			writer.Write (" (id self, SEL sel");
			if (func.Parameters != null) {
				for (int i = 0; i < func.Parameters.Length; i++) {
					writer.Write (", {0} p{1}", func.Parameters [i].TypeData.NativeWrapperType, i);
				}
			}
			writer.WriteLine (")");

			// body
			writer.WriteLine ("{");
			if (func.ReturnType != null && func.ReturnType.RequireMarshal) {
				writer.WriteLine ("\t{0} rv;", func.ReturnType.NativeType);
			}

			// marshal managed parameters to native format
			WriteParametersMarshal (writer, func.Parameters);

			if (func.ReturnType != null && func.ReturnType.IsAnyStret)
				WriteMessageStretSenderCode (writer, func.ReturnType, false);
			
			// @try
			string indent = "\t";
			if (func.MarshalExceptions) {
				writer.WriteLine ("\t@try {");
				indent = "\t\t";
			}

			// invoke
			writer.Write (indent);
			if (func.ReturnType != null) {
				if (func.ReturnType.RequireMarshal) {
					writer.Write ("rv = ");
				} else {
					writer.Write ("return ");
				}
			}
			writer.Write ("((func_{0}) {1}) (self, sel", funcName, (func.ReturnType != null && func.ReturnType.IsAnyStret) ? "msgSend" : overload);
			WriteParametersInvoke (writer, func.Parameters);
			writer.WriteLine (");");

			if (func.ReturnType != null) {
				if (func.ReturnType.RequireMarshal) {
					// Marshal return value back
					writer.WriteLine ("{0}{1} rvm;", indent, func.ReturnType.NativeWrapperType);
					MarshalToManaged (writer, func.ReturnType, "rv", "rvm");
					writer.WriteLine ("{0}return rvm;", indent);
				}
			}

			// @catch
			if (func.MarshalExceptions) {
				writer.WriteLine ("\t} @catch (NSException *e) {");
				writer.WriteLine ("\t\txamarin_throw_ns_exception (e);");
				writer.WriteLine ("\t}");
			}

			writer.WriteLine ("}");
			writer.WriteLine ();
		}
Example #3
0
		static void Write_objc_msgSendSuper_stret (StringWriter writer, FunctionData func)
		{
			if (func.ReturnType == null)
				throw new Exception (string.Format ("stret functions must have a return type: {0}", func.Comment));
			
			// func name
			var overload = "objc_msgSendSuper_stret";
			var funcName = GetFuncName (func, overload);

			// typedef
			writer.Write ("typedef {0} (*func_{1}) (struct objc_super *super, SEL sel", func.ReturnType.NativeType, funcName.ToString ());
			if (func.Parameters != null) {
				for (int i = 0; i < func.Parameters.Length; i++) {
					writer.Write (", {0} f{1}", func.Parameters [i].TypeData.NativeType, i);
				}
			}
			writer.WriteLine (");");

			// declaration
			writer.WriteLine ("void");
			writer.Write (funcName);
			writer.Write (" ({0} *stret_rv, struct objc_super *super, SEL sel", func.ReturnType.NativeWrapperType);
			if (func.Parameters != null) {
				for (int i = 0; i < func.Parameters.Length; i++) {
					writer.Write (", {0} p{1}", func.Parameters [i].TypeData.NativeWrapperType, i);
				}
			}
			writer.WriteLine (")");

			// body
			writer.WriteLine ("{");
			if (func.ReturnType.RequireMarshal)
				writer.WriteLine ("\t{0} rv;", func.ReturnType.NativeType);

			// marshal managed parameters to native format
			WriteParametersMarshal (writer, func.Parameters);

			if (func.ReturnType.IsAnyStret)
				WriteMessageStretSenderCode (writer, func.ReturnType, true);

			// @try
			string indent = "\t";
			if (func.MarshalExceptions) {
				writer.WriteLine ("\t@try {");
				indent = "\t\t";
			}

			// invoke
			writer.Write (indent);
			if (func.ReturnType.RequireMarshal) {
				writer.Write ("rv = ");
			} else {
				writer.Write ("*stret_rv = ");
			}
			writer.Write ("((func_{0}) {1}) (super, sel", funcName, func.ReturnType.IsAnyStret ? "msgSend" : overload.Replace ("_stret", ""));
			WriteParametersInvoke (writer, func.Parameters);
			writer.WriteLine (");");

			if (func.ReturnType.RequireMarshal) {
				// Marshal return value back
				MarshalToManaged (writer, func.ReturnType, "rv", "(*stret_rv)");
			}

			// @catch
			if (func.MarshalExceptions) {
				writer.WriteLine ("\t} @catch (NSException *e) {");
				writer.WriteLine ("\t\txamarin_throw_ns_exception (e);");
				writer.WriteLine ("\t}");
			}

			writer.WriteLine ("}");
			writer.WriteLine ();
		}
Example #4
0
		static string GetFuncName (FunctionData func, string overload)
		{
			var funcName = new StringBuilder ();
			funcName.Append ("xamarin_");
			funcName.Append (func.Prefix);
			if (func.ReturnType != null) {
				funcName.Append (GetTypeNameForSignature (func.ReturnType.ManagedType));
			} else {
				funcName.Append ("void");
			}
			funcName.Append ("_").Append (overload);
			if (func.Parameters != null) {
				for (int i = 0; i < func.Parameters.Length; i++) {
					funcName.Append ("_");
					if (func.Parameters [i].IsRef)
						funcName.Append ("ref_");
					funcName.Append (GetTypeNameForSignature (func.Parameters [i].TypeData.ManagedType));
				}
			}
			return funcName.ToString ();
		}