YAKOVM
YAKOVM

Reputation: 10153

define an enum using include

I define an enums using include,since there are different enums which have the same enumeration data and I want to reuse it:

#define X(SYM) SYM
#define X_INIT(SYM,VAL) SYM = VAL
/// Destination for scalar memory read instruction
enum SSRC
{

#include "GenericInstructionFields1.h"
#include "ScalarInstructionFields.h"
#include "GenericInstructionFields2.h"

};
enum SDST
{

        #include "GenericInstructionFields1.h"
};

#undef X_INIT
#undef X
};     

But I can`t compile the code for SDST. The compiler writes redefinition for a fields of SSRC,which comes from "GenericInstructionFields1.h". What is the cause of the problem and how can it be solved?

//GenericInstructionFields1.h
/// SGPR0 to SGPR103: Scalar general-purpose registers.
X_INIT(ScalarGPRMin,0),
X(ScalarGPR),
X_INIT(ScalarGPRMax,103),
/// 104 – 105 reserved.
X(Reserved104),
X(Reserved105),
X_INIT(Reserved,2),
/// vcc[31:0].
X_INIT(VccLo, 106),
/// vcc[63:32].
X(VccHi),

Upvotes: 1

Views: 1452

Answers (2)

user93353
user93353

Reputation: 14039

Enums are not like namespaces.

You will see the same error with the following

enum A
{
    P, Q
};

enum B
{
    P, Q
};

You can achieve what you want by this

struct A
{
    enum { P, Q };
};

struct B
{
    enum { P, Q };
};

You can now use A::P, A::Q, B::P & B::Q

Or in your case

#define X(SYM) SYM
#define X_INIT(SYM,VAL) SYM = VAL
/// Destination for scalar memory read instruction

struct SSRC
{
    enum
    {

        #include "GenericInstructionFields1.h"
        #include "ScalarInstructionFields.h"
        #include "GenericInstructionFields2.h"

    }

};

struct SDST
{
    enum 
    {
        #include "GenericInstructionFields1.h"
    }
};

#undef X_INIT
#undef X
};

You can now use SSRC::ScalarGPRMax and SDST::ScalarGPRMax

Upvotes: 1

dhavenith
dhavenith

Reputation: 2048

You can't have enumerations with the same enumerators in the same namespace. This would reproduce your problem:

enum X {A,B};
enum Y {A};

either use namespaces or prefix your enum values.

Upvotes: 4

Related Questions