Subject: bug#15880: Compute C declarations for DEFSYMs automatically.

From: Paul Eggert <[email-masked]>
Date: Fri, 09 Jan 2015 08:33:53 -0800

Eli Zaretskii wrote:
> Could we instead just keep the previous semantics of XIL and XLI,
> please?  They are used in a few places outside lisp.h, so now all of
> those places need to be audited and most probably changed.  Why isn't
> it better to change the implementation of just 2 macros instead?

XIL and XLI haven't changed, and have the same semantics as before: they convert 
between Lisp_Object and EMACS_INT without altering the bits (i.e., they are 
no-ops at the machine level).  I expect it's better to keep them no-ops, for 
efficiency in the kernel of the Emacs Lisp interpreter.

I audited all the places XIL and XLI are used, and the uses in non-w32 code are 
all fine.  I don't know the w32 code, but most likely some or all of its uses 
need to be changed.  I moved the new conversion operations to lisp.h (installed 
on master) and wrote the attached untested patch for the w32 code.

>From 126c396d0b780742c785461d82d3b1127d492086 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Fri, 9 Jan 2015 08:21:19 -0800
Subject: [PATCH] Fix pointer-to-integer conversion in w32 code

* w32fns.c (w32_msg_pump, Fw32_register_hot_key)
(Fw32_unregister_hot_key, Fw32_toggle_lock_key):
* w32inevt.c (handle_file_notifications):
* w32menu.c (add_menu_item, w32_menu_display_help):
* w32notify.c (Fw32notify_add_watch, Fw32notify_rm_watch)
(w32_get_watch_object):
* w32term.c (queue_notifications):
Use make_pointer_integer and XINTPTR rather than XIL and XLI.
---
 src/ChangeLog   | 10 ++++++++++
 src/w32fns.c    | 10 +++++-----
 src/w32inevt.c  |  4 +++-
 src/w32menu.c   |  6 ++++--
 src/w32notify.c |  6 +++---
 src/w32term.c   |  5 +++--
 6 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index c11ba11..22e690c 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,15 @@
 2015-01-09  Paul Eggert  <[email-masked]>
 
+	Fix pointer-to-integer conversion in w32 code
+	* w32fns.c (w32_msg_pump, Fw32_register_hot_key)
+	(Fw32_unregister_hot_key, Fw32_toggle_lock_key):
+	* w32inevt.c (handle_file_notifications):
+	* w32menu.c (add_menu_item, w32_menu_display_help):
+	* w32notify.c (Fw32notify_add_watch, Fw32notify_rm_watch)
+	(w32_get_watch_object):
+	* w32term.c (queue_notifications):
+	Use make_pointer_integer and XINTPTR rather than XIL and XLI.
+
 	Refactor pointer-to-integer conversion
 	* gfilenotify.c (monitor_to_lisp, lisp_to_monitor):
 	Rename and move to lisp.h.  All uses changed.
diff --git a/src/w32fns.c b/src/w32fns.c
index ced3d87..c6cdf96 100644
--- a/src/w32fns.c
+++ b/src/w32fns.c
@@ -2540,7 +2540,7 @@ w32_msg_pump (deferred_msg * msg_buf)
                  thread-safe.  The next line is okay because the cons
                  cell is never made into garbage and is not relocated by
                  GC.  */
-	      XSETCAR (XIL ((EMACS_INT) msg.lParam), Qnil);
+	      XSETCAR (make_pointer_integer (msg.lParam), Qnil);
 	      if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
 		emacs_abort ();
 	      break;
@@ -2548,7 +2548,7 @@ w32_msg_pump (deferred_msg * msg_buf)
 	      {
 		int vk_code = (int) msg.wParam;
 		int cur_state = (GetKeyState (vk_code) & 1);
-		Lisp_Object new_state = XIL ((EMACS_INT) msg.lParam);
+		Lisp_Object new_state = make_pointer_integer (msg.lParam);
 
 		/* NB: This code must be thread-safe.  It is safe to
                    call NILP because symbols are not relocated by GC,
@@ -7235,7 +7235,7 @@ The return value is the hotkey-id if registered, otherwise nil.  */)
       /* Notify input thread about new hot-key definition, so that it
 	 takes effect without needing to switch focus.  */
       PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
-			 (WPARAM) XLI (key), 0);
+			 XINTPTR (key), 0);
     }
 
   return key;
@@ -7258,7 +7258,7 @@ DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
       /* Notify input thread about hot-key definition being removed, so
 	 that it takes effect without needing focus switch.  */
       if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
-			     (WPARAM) XINT (XCAR (item)), (LPARAM) XLI (item)))
+			     (WPARAM) XINT (XCAR (item)), XINTPTR (item)))
 	{
 	  MSG msg;
 	  GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
@@ -7331,7 +7331,7 @@ is set to off if the low bit of NEW-STATE is zero, otherwise on.  */)
     return make_number (w32_console_toggle_lock_key (vk_code, new_state));
 
   if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
-			 (WPARAM) vk_code, (LPARAM) XLI (new_state)))
+			 (WPARAM) vk_code, XINTPTR (new_state)))
     {
       MSG msg;
       GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
diff --git a/src/w32inevt.c b/src/w32inevt.c
index daf4a5c..ef2ec35 100644
--- a/src/w32inevt.c
+++ b/src/w32inevt.c
@@ -655,9 +655,11 @@ handle_file_notifications (struct input_event *hold_quit)
 	      Lisp_Object fname
 		= code_convert_string_norecord (utf_16_fn, cs, 0);
 	      Lisp_Object action = lispy_file_action (fni->Action);
+	      Lisp_Object descriptor
+		= make_pointer_integer (notifications_desc);
 
 	      inev.kind = FILE_NOTIFY_EVENT;
-	      inev.code = (ptrdiff_t)XINT (XIL ((EMACS_INT)notifications_desc));
+	      inev.code = XINT (descriptor);
 	      inev.timestamp = GetTickCount ();
 	      inev.modifiers = 0;
 	      inev.frame_or_window = callback;
diff --git a/src/w32menu.c b/src/w32menu.c
index 7a946d2..3ec6b54 100644
--- a/src/w32menu.c
+++ b/src/w32menu.c
@@ -1412,7 +1412,7 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
 		 be ULONG_PTR, which is correct for 32-bit and 64-bit
 		 Windows alike.  MSVC headers get it right; hopefully,
 		 MinGW headers will, too.  */
-	      info.dwItemData = (ULONG_PTR) XLI (wv->help);
+	      info.dwItemData = XINTPTR (wv->help);
 	    }
 	  if (wv->button_type == BUTTON_TYPE_RADIO)
 	    {
@@ -1488,7 +1488,9 @@ w32_menu_display_help (HWND owner, HMENU menu, UINT item, UINT flags)
 	  info.fMask = MIIM_DATA;
 	  get_menu_item_info (menu, item, FALSE, &info);
 
-	  help = info.dwItemData ? XIL (info.dwItemData) : Qnil;
+	  help = (info.dwItemData
+		  ? make_pointer_integer (info.dwItemData)
+		  : Qnil);
 	}
 
       /* Store the help echo in the keyboard buffer as the X toolkit
diff --git a/src/w32notify.c b/src/w32notify.c
index a0d555b..27d0aa1 100644
--- a/src/w32notify.c
+++ b/src/w32notify.c
@@ -580,7 +580,7 @@ generate notifications correctly, though.  */)
 	report_file_error ("Cannot watch file", Fcons (file, Qnil));
     }
   /* Store watch object in watch list. */
-  watch_descriptor = XIL ((EMACS_INT)dirwatch);
+  watch_descriptor = make_pointer_integer (dirwatch);
   watch_object = Fcons (watch_descriptor, callback);
   watch_list = Fcons (watch_object, watch_list);
 
@@ -605,7 +605,7 @@ WATCH-DESCRIPTOR should be an object returned by `w32notify-add-watch'.  */)
   if (!NILP (watch_object))
     {
       watch_list = Fdelete (watch_object, watch_list);
-      dirwatch = (struct notification *)XLI (watch_descriptor);
+      dirwatch = XINTPTR (watch_descriptor);
       if (w32_valid_pointer_p (dirwatch, sizeof(struct notification)))
 	status = remove_watch (dirwatch);
     }
@@ -620,7 +620,7 @@ WATCH-DESCRIPTOR should be an object returned by `w32notify-add-watch'.  */)
 Lisp_Object
 w32_get_watch_object (void *desc)
 {
-  Lisp_Object descriptor = XIL ((EMACS_INT)desc);
+  Lisp_Object descriptor = make_pointer_integer (desc);
 
   /* This is called from the input queue handling code, inside a
      critical section, so we cannot possibly QUIT if watch_list is not
diff --git a/src/w32term.c b/src/w32term.c
index 8a53a58..7ac3120 100644
--- a/src/w32term.c
+++ b/src/w32term.c
@@ -3245,10 +3245,11 @@ queue_notifications (struct input_event *event, W32Msg *msg, struct frame *f,
 	      Lisp_Object fname
 		= code_convert_string_norecord (utf_16_fn, cs, 0);
 	      Lisp_Object action = lispy_file_action (fni->Action);
+	      Lisp_Object descriptor
+		= make_pointer_integer (notifications_desc);
 
 	      event->kind = FILE_NOTIFY_EVENT;
-	      event->code
-		= (ptrdiff_t)XINT (XIL ((EMACS_INT)notifications_desc));
+	      event->code = XINT (descriptor);
 	      event->timestamp = msg->msg.time;
 	      event->modifiers = 0;
 	      event->frame_or_window = callback;
-- 
2.1.0