[m-rev.] diff: implement io__stream as foreign type

Peter Ross peter.ross at miscrit.be
Wed May 8 21:15:56 AEST 2002


Hi,

Once this bootchecks, I will check it in.


===================================================================


Estimated hours taken: 4
Branches: main

Change the type io__stream to be implemented using pragma foreign_type.
This should make it easier to port the library to the il grade.

library/io.m:
    Implement the type io__stream using pragma foreign_type.

Index: library/io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.251
diff -u -r1.251 io.m
--- library/io.m	25 Mar 2002 16:50:04 -0000	1.251
+++ library/io.m	8 May 2002 09:24:25 -0000
@@ -1318,7 +1318,10 @@
 
 :- type io__binary_stream ==	io__stream.
 
-:- type io__stream == c_pointer.
+:- type io__stream.
+:- pragma foreign_type(c, io__stream, "MercuryFile *").
+:- pragma foreign_type(il, io__stream,
+		"class [mercury]mercury.io__cpp_code.MR_MercuryFileStruct").
 
 	% a unique identifier for an IO stream
 :- type io__stream_id == int.
@@ -1897,8 +1900,7 @@
 		IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, thread_safe],
 "{
-	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile mf = Stream;
 	if (mf->stream->get_CanSeek()) {
 		Size = mf->stream->get_Length();
 	} else {
@@ -3088,8 +3090,7 @@
 :- pragma foreign_proc("MC++",
 	io__get_stream_id(Stream::in, Id::out), 
 		[will_not_call_mercury, promise_pure], "
-	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile,
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile mf = Stream;
 	Id = mf->id;
 ").
 
@@ -3926,8 +3927,7 @@
 :- pragma foreign_proc("MC++", 
 	io__read_char_code(File::in, CharCode::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure], "
-	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(File));
+	MR_MercuryFile mf = File;
 	CharCode = mercury_getc(mf);
 	update_io(IO0, IO);
 ").
@@ -3936,8 +3936,7 @@
 	io__putback_char(File::in, Character::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure], "{
 
-	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile,
-		MR_word_to_c_pointer(File));
+	MR_MercuryFile mf = File;
 	if (Character == '\\n') {
 		mf->line_number--;
 	}
@@ -3949,8 +3948,7 @@
 	io__putback_byte(File::in, _Character::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure], "{
 
-	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(File));
+	MR_MercuryFile mf = File;
 	mf->stream->Seek(-1, System::IO::SeekOrigin::Current);
 	update_io(IO0, IO);
 }").
@@ -4283,8 +4281,7 @@
 	io__write_string(Stream::in, Message::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, thread_safe, tabled_for_io], 
 "{
-	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile stream = Stream;
 	mercury_print_string(stream, Message);
 	update_io(IO0, IO);
 }").
@@ -4293,8 +4290,7 @@
 	io__write_char(Stream::in, Character::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, thread_safe, tabled_for_io], 
 "{
-	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile stream = Stream;
 	System::IO::StreamWriter *w = new System::IO::StreamWriter(
 		mercury_current_binary_output->stream);
 	w->Write(Character);
@@ -4306,8 +4302,7 @@
 	io__write_int(Stream::in, Val::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "{
-	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile stream = Stream;
 	System::IO::StreamWriter *w = new System::IO::StreamWriter(
 		mercury_current_binary_output->stream);
 	w->Write(Val.ToString());
@@ -4319,8 +4314,7 @@
 	io__write_float(Stream::in, Val::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "{
-	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile stream = Stream;
 	System::IO::StreamWriter *w = new System::IO::StreamWriter(
 		mercury_current_binary_output->stream);
 	w->Write(Val.ToString());
@@ -4334,8 +4328,7 @@
 "{
 	mercury::runtime::Errors::SORRY(""foreign code for this function"");
 		// something like this...
-	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile stream = Stream;
 	System::IO::StreamWriter *w = new System::IO::StreamWriter(
 		mercury_current_binary_output->stream);
 	w->Write(Byte.ToString());
@@ -4347,8 +4340,7 @@
 	io__write_bytes(Stream::in, Message::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "{
-	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile stream = Stream;
 	mercury_print_binary_string(stream, Message);
 	update_io(IO0, IO);
 }").
@@ -4357,8 +4349,7 @@
 	io__flush_output(Stream::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "{
-	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile stream = Stream;
 	stream->stream->Flush();
 	update_io(IO0, IO);
 }").
@@ -4367,8 +4358,7 @@
 	io__flush_binary_output(Stream::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "{
-	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile stream = Stream;
 	stream->stream->Flush();
 	update_io(IO0, IO);
 }").
@@ -4385,7 +4375,7 @@
 		[will_not_call_mercury, promise_pure, tabled_for_io,
 			thread_safe],
 "
-	Stream = (MR_Word) &mercury_stdin;
+	Stream = &mercury_stdin;
 	update_io(IO0, IO);
 ").
 
@@ -4394,7 +4384,7 @@
 		[will_not_call_mercury, promise_pure, tabled_for_io,
 			thread_safe],
 "
-	Stream = (MR_Word) &mercury_stdout;
+	Stream = &mercury_stdout;
 	update_io(IO0, IO);
 ").
 
@@ -4403,7 +4393,7 @@
 		[will_not_call_mercury, promise_pure, tabled_for_io,
 			thread_safe],
 "
-	Stream = (MR_Word) &mercury_stderr;
+	Stream = &mercury_stderr;
 	update_io(IO0, IO);
 ").
 
@@ -4412,7 +4402,7 @@
 		[will_not_call_mercury, promise_pure, tabled_for_io,
 			thread_safe],
 "
-	Stream = (MR_Word) &mercury_stdin_binary;
+	Stream = &mercury_stdin_binary;
 	update_io(IO0, IO);
 ").
 
@@ -4421,7 +4411,7 @@
 		[will_not_call_mercury, promise_pure, tabled_for_io,
 			thread_safe],
 "
-	Stream = (MR_Word) &mercury_stdout_binary;
+	Stream = &mercury_stdout_binary;
 	update_io(IO0, IO);
 ").
 
@@ -4429,7 +4419,7 @@
 	io__input_stream(Stream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	Stream = (MR_Word) mercury_current_text_input;
+	Stream = mercury_current_text_input;
 	update_io(IO0, IO);
 ").
 
@@ -4437,7 +4427,7 @@
 	io__output_stream(Stream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	Stream = (MR_Word) mercury_current_text_output;
+	Stream = mercury_current_text_output;
 	update_io(IO0, IO);
 ").
 
@@ -4445,7 +4435,7 @@
 	io__binary_input_stream(Stream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	Stream = (MR_Word) mercury_current_binary_input;
+	Stream = mercury_current_binary_input;
 	update_io(IO0, IO);
 ").
 
@@ -4453,7 +4443,7 @@
 	io__binary_output_stream(Stream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	Stream = (MR_Word) mercury_current_binary_output;
+	Stream = mercury_current_binary_output;
 	update_io(IO0, IO);
 ").
 
@@ -4530,7 +4520,7 @@
 	io__current_input_stream(OutStream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_text_input;
+	OutStream = mercury_current_text_input;
 	update_io(IO0, IO);
 ").
 
@@ -4538,7 +4528,7 @@
 	io__current_output_stream(OutStream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_text_output;
+	OutStream = mercury_current_text_output;
 	update_io(IO0, IO);
 ").
 
@@ -4546,7 +4536,7 @@
 	io__current_binary_input_stream(OutStream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_binary_input;
+	OutStream = mercury_current_binary_input;
 	update_io(IO0, IO);
 ").
 
@@ -4554,7 +4544,7 @@
 	io__current_binary_output_stream(OutStream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_binary_output;
+	OutStream = mercury_current_binary_output;
 	update_io(IO0, IO);
 ").
 
@@ -4566,7 +4556,7 @@
 		IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_text_input;
+	OutStream = mercury_current_text_input;
 	mercury_current_text_input = (MercuryFile *) NewStream;
 	update_io(IO0, IO);
 ").
@@ -4576,7 +4566,7 @@
 		IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_text_output;
+	OutStream = mercury_current_text_output;
 	mercury_current_text_output = (MercuryFile *) NewStream;
 	update_io(IO0, IO);
 ").
@@ -4586,7 +4576,7 @@
 		IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_binary_input;
+	OutStream = mercury_current_binary_input;
 	mercury_current_binary_input = (MercuryFile *) NewStream;
 	update_io(IO0, IO);
 ").
@@ -4596,7 +4586,7 @@
 		IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	OutStream = (MR_Word) mercury_current_binary_output;
+	OutStream = mercury_current_binary_output;
 	mercury_current_binary_output = (MercuryFile *) NewStream;
 	update_io(IO0, IO);
 ").
@@ -4606,7 +4596,7 @@
 		[will_not_call_mercury, promise_pure, thread_safe,
 			tabled_for_io],
 "
-	MR_c_pointer_to_word(Stream, mercury_stdin);
+	Stream = mercury_stdin;
 	update_io(IO0, IO);
 ").
 
@@ -4615,7 +4605,7 @@
 		[will_not_call_mercury, promise_pure, thread_safe,
 			tabled_for_io],
 "
-	MR_c_pointer_to_word(Stream, mercury_stdout);
+	Stream = mercury_stdout;
 	update_io(IO0, IO);
 ").
 
@@ -4624,7 +4614,7 @@
 		[will_not_call_mercury, promise_pure, thread_safe,
 			tabled_for_io],
 "
-	MR_c_pointer_to_word(Stream, mercury_stderr);
+	Stream = mercury_stderr;
 	update_io(IO0, IO);
 ").
 
@@ -4633,7 +4623,7 @@
 		[will_not_call_mercury, promise_pure, thread_safe,
 			tabled_for_io],
 "
-	MR_c_pointer_to_word(Stream, mercury_stdin_binary);
+	Stream = mercury_stdin_binary;
 	update_io(IO0, IO);
 ").
 
@@ -4642,7 +4632,7 @@
 		[will_not_call_mercury, promise_pure, thread_safe,
 			tabled_for_io],
 "
-	MR_c_pointer_to_word(Stream, mercury_stdout_binary);
+	Stream = mercury_stdout_binary;
 	update_io(IO0, IO);
 ").
 
@@ -4650,7 +4640,7 @@
 	io__input_stream(Stream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	MR_c_pointer_to_word(Stream, mercury_current_text_input);
+	Stream = mercury_current_text_input;
 	update_io(IO0, IO);
 ").
 
@@ -4658,7 +4648,7 @@
 	io__output_stream(Stream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	MR_c_pointer_to_word(Stream, mercury_current_text_output);
+	Stream = mercury_current_text_output;
 	update_io(IO0, IO);
 ").
 
@@ -4666,7 +4656,7 @@
 	io__binary_input_stream(Stream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	MR_c_pointer_to_word(Stream, mercury_current_binary_input);
+	Stream = mercury_current_binary_input;
 	update_io(IO0, IO);
 ").
 
@@ -4674,7 +4664,7 @@
 	io__binary_output_stream(Stream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "
-	MR_c_pointer_to_word(Stream, mercury_current_binary_output);
+	Stream = mercury_current_binary_output;
 	update_io(IO0, IO);
 ").
 
@@ -4690,8 +4680,7 @@
 	io__get_line_number(Stream::in, LineNum::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "{
-	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile stream = Stream;
 	LineNum = stream->line_number;
 	update_io(IO0, IO);
 }").
@@ -4708,8 +4697,7 @@
 	io__set_line_number(Stream::in, LineNum::in, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io],
 "{
-	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile stream = Stream;
 	stream->line_number = LineNum;
 	update_io(IO0, IO);
 }").
@@ -4724,8 +4712,7 @@
 :- pragma foreign_proc("MC++",
 	io__get_output_line_number(Stream::in, LineNum::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io], "{
-	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile stream = Stream;
 	LineNum = stream->line_number;
 	update_io(IO0, IO);
 }").
@@ -4740,8 +4727,7 @@
 :- pragma foreign_proc("MC++",
 	io__set_output_line_number(Stream::in, LineNum::in, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io], "{
-	MR_MercuryFile stream = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile stream = Stream;
 	stream->line_number = LineNum;
 	update_io(IO0, IO);
 }").
@@ -4752,18 +4738,16 @@
 :- pragma foreign_proc("MC++",
 	io__set_input_stream(NewStream::in, OutStream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io], "
-	MR_c_pointer_to_word(OutStream, mercury_current_text_input);
-	mercury_current_text_input = 
-		ML_DownCast(MR_MercuryFile, MR_word_to_c_pointer(NewStream));
+	OutStream = mercury_current_text_input;
+	mercury_current_text_input = NewStream;
 	update_io(IO0, IO);
 ").
 
 :- pragma foreign_proc("MC++",
 	io__set_output_stream(NewStream::in, OutStream::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure, tabled_for_io], "
-	MR_c_pointer_to_word(OutStream, mercury_current_text_output);
-	mercury_current_text_output = 
-		ML_DownCast(MR_MercuryFile, MR_word_to_c_pointer(NewStream));
+	OutStream = mercury_current_text_output;
+	mercury_current_text_output = NewStream;
 	update_io(IO0, IO);
 ").
 
@@ -4771,9 +4755,8 @@
 	io__set_binary_input_stream(NewStream::in, OutStream::out,
 		IO0::di, IO::uo), 
 		[will_not_call_mercury, promise_pure, tabled_for_io], "
-	MR_c_pointer_to_word(OutStream, mercury_current_binary_input);
-	mercury_current_binary_input = 
-		ML_DownCast(MR_MercuryFile, MR_word_to_c_pointer(NewStream));
+	OutStream = mercury_current_binary_input;
+	mercury_current_binary_input = NewStream;
 	update_io(IO0, IO);
 ").
 
@@ -4781,9 +4764,8 @@
 	io__set_binary_output_stream(NewStream::in, OutStream::out,
 		IO0::di, IO::uo), 
 		[will_not_call_mercury, promise_pure, tabled_for_io], "
-	MR_c_pointer_to_word(OutStream, mercury_current_binary_output);
-	mercury_current_binary_output = 
-		ML_DownCast(MR_MercuryFile, MR_word_to_c_pointer(NewStream));
+	OutStream = mercury_current_binary_output;
+	mercury_current_binary_output = NewStream;
 	update_io(IO0, IO);
 ").
 
@@ -4799,7 +4781,7 @@
 		[will_not_call_mercury, promise_pure, tabled_for_io,
 			thread_safe],
 "
-	Stream = (MR_Word) mercury_open(FileName, Mode);
+	Stream = mercury_open(FileName, Mode);
 	update_io(IO0, IO);
 ").
@@ -4811,7 +4793,7 @@
 			thread_safe],
 "
 	MR_MercuryFile mf = mercury_open(FileName, Mode);
-	MR_c_pointer_to_word(Stream, mf);
+	Stream = mf;
 	update_io(IO0, IO);
 ").
@@ -4842,8 +4824,7 @@
 
 :- pragma foreign_proc("MC++", io__close_stream(Stream::in, IO0::di, IO::uo),
 		[may_call_mercury, promise_pure, tabled_for_io, thread_safe], "
-	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile, 
-		MR_word_to_c_pointer(Stream));
+	MR_MercuryFile mf = Stream;
 	mercury_close(mf);
 	update_io(IO0, IO);
 ").

--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list