unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* enum may be signed
@ 2010-05-01 15:47 grischka
  2010-05-01 17:56 ` Stefan Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: grischka @ 2010-05-01 15:47 UTC (permalink / raw)
  To: emacs-devel

In git:2f2ce26ff273aadaf324ec599bf1dd1f67cab560 (Stefan Monnier)

+enum symbol_redirect
+{
+  SYMBOL_PLAINVAL  = 4,
...

  struct Lisp_Symbol
  {
    unsigned gcmarkbit : 1;
...
+  enum symbol_redirect redirect : 3;


Note that this breaks with compilers where enums can be signed (e.g. MSVC),
Because value 4 doesn't fit into 3 bits (signed), it is read as -4 and
breaks related switch statements (e.g. data.c:set_internal)

--- grischka





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: enum may be signed
  2010-05-01 15:47 enum may be signed grischka
@ 2010-05-01 17:56 ` Stefan Monnier
  2010-05-01 18:37   ` Ken Raeburn
                     ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Stefan Monnier @ 2010-05-01 17:56 UTC (permalink / raw)
  To: grischka; +Cc: emacs-devel

> +  enum symbol_redirect redirect : 3;

> Note that this breaks with compilers where enums can be signed (e.g. MSVC),
> Because value 4 doesn't fit into 3 bits (signed), it is read as -4 and
> breaks related switch statements (e.g. data.c:set_internal)

Interesting.  I didn't know about that part of the C language.  Is it
possible to specify that this is an unsigned enum somehow?
I've tried "enum symbol_redirect redirect : 3;" but at least GCC didn't
like it.
Of course we can add one more bit (we have plenty of bits left there),
but I'd first like to know how this is usually handled, since it seems
to imply that MSVC always requires one extra bit to store enums.


        Stefan




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: enum may be signed
  2010-05-01 17:56 ` Stefan Monnier
@ 2010-05-01 18:37   ` Ken Raeburn
  2010-05-01 18:51   ` grischka
  2010-05-03 17:04   ` Tom Tromey
  2 siblings, 0 replies; 5+ messages in thread
From: Ken Raeburn @ 2010-05-01 18:37 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: grischka, emacs-devel

On May 1, 2010, at 13:56, Stefan Monnier wrote:
> Interesting.  I didn't know about that part of the C language.  Is it
> possible to specify that this is an unsigned enum somehow?

Not really... nothing in standard C supports it (in fact IIRC it doesn't allow enum bitfields anyways, but that's a common extension), and anything MSVC-specific won't help when the next such compiler comes along.

> Of course we can add one more bit (we have plenty of bits left there),
> but I'd first like to know how this is usually handled, since it seems
> to imply that MSVC always requires one extra bit to store enums.

I think adding the extra bit and using "unsigned int" bitfields are the two usual ways to deal with it.

Ken



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: enum may be signed
  2010-05-01 17:56 ` Stefan Monnier
  2010-05-01 18:37   ` Ken Raeburn
@ 2010-05-01 18:51   ` grischka
  2010-05-03 17:04   ` Tom Tromey
  2 siblings, 0 replies; 5+ messages in thread
From: grischka @ 2010-05-01 18:51 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Stefan Monnier wrote:
>> +  enum symbol_redirect redirect : 3;
> 
>> Note that this breaks with compilers where enums can be signed (e.g. MSVC),
>> Because value 4 doesn't fit into 3 bits (signed), it is read as -4 and
>> breaks related switch statements (e.g. data.c:set_internal)
> 
> Interesting.  I didn't know about that part of the C language.  Is it
> possible to specify that this is an unsigned enum somehow?
> I've tried "enum symbol_redirect redirect : 3;" but at least GCC didn't
> like it.

Planned for C++0x:
http://en.wikipedia.org/wiki/C%2B%2B0x#Strongly_typed_enumerations

> Of course we can add one more bit (we have plenty of bits left there),
> but I'd first like to know how this is usually handled, since it seems
> to imply that MSVC always requires one extra bit to store enums.

Good old "unsigned redirect : 3;" works as expected.

Btw, check also "enum Lisp_Type type : GCTYPEBITS;".

--- grischka

> 
> 
>         Stefan
> 





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: enum may be signed
  2010-05-01 17:56 ` Stefan Monnier
  2010-05-01 18:37   ` Ken Raeburn
  2010-05-01 18:51   ` grischka
@ 2010-05-03 17:04   ` Tom Tromey
  2 siblings, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2010-05-03 17:04 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: grischka, emacs-devel

>>>>> "Stefan" == Stefan Monnier <monnier@iro.umontreal.ca> writes:

Stefan> Interesting.  I didn't know about that part of the C language.  Is it
Stefan> possible to specify that this is an unsigned enum somehow?
Stefan> I've tried "enum symbol_redirect redirect : 3;" but at least GCC didn't
Stefan> like it.
Stefan> Of course we can add one more bit (we have plenty of bits left there),
Stefan> but I'd first like to know how this is usually handled, since it seems
Stefan> to imply that MSVC always requires one extra bit to store enums.

GCC and GDB do something like this:

/* Be conservative and use enum bitfields only with GCC.
   This is copied from gcc 3.3.1, system.h.  */

#if defined(__GNUC__) && (__GNUC__ >= 2)
#define ENUM_BITFIELD(TYPE) enum TYPE
#else
#define ENUM_BITFIELD(TYPE) unsigned int
#endif


Used like:

struct main_type
{
  /* Code for kind of type */

  ENUM_BITFIELD(type_code) code : 8;
...



Continuing to use an enum bitfield on gcc is nice, because gcc will warn
if the enum is too large to fit in the bitfield.

Tom




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-05-03 17:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-01 15:47 enum may be signed grischka
2010-05-01 17:56 ` Stefan Monnier
2010-05-01 18:37   ` Ken Raeburn
2010-05-01 18:51   ` grischka
2010-05-03 17:04   ` Tom Tromey

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).