Re: [cc65] Struct embedded enum

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2003-10-09 20:12:34


Hi!

On Thu, Oct 09, 2003 at 06:43:24AM -0400, Keates, Mark wrote:
> struct threadQueue {
>     ...
>     enum {
>  	THREAD_JUST_BORN = 0,	/* Not "start"ed yet */
>  	THREAD_ACTIVE = 1,	/* Currently running, or on Run queue */
>  	THREAD_SUSPENDED = 2,   /* Waiting for monitor or alarm */
>  	THREAD_DEAD = 3		/* Thread has quit */
>     } state;
> };
[...]
> The question is, is there a specific way that the enum label could be
> referenced?

C is rather inconsequent in regard to scoping. While struct members are local
to the struct, other structs and enums are not. This means that

    struct x {
        int foo;
        struct y {
            int bar;
        } Y;
    };

    struct y Y;

is legal C. The introduction of struct y in the scope of struct x is
misleading, it is actually globally visible. The same is true for enums.

cc65 is wrong when it comes to enums, it will add the enum tags to the local
scope of the struct, which is the way it is handled in most other languages.
I'm going to fix this, in the meantime, you may use

    if (CurrentThread->state == threadQueue.THREAD_ACTIVE) {

as a workaround.

> I guess a better way would be to typedef the enum outside of the struct and
> then
> use then declare 'state' properly within the struct?

There shouldn't be a difference for the compiler, but it makes things clearer
for the programmer/maintainer. The declaration of threadQueue suggests that
the enum is local to the struct, but it is in fact a global type.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz_at_musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2003-10-09 20:12:46 CEST