Array with size of an Enum Constant

Dear all,

I have an enum declaration and a structure declaration of following kind:

typedef enum
{
BITS_HEADER,
BITS_TOTAL_MB,
BITS_MB_MODE,
BITS_INTER_MB,
BITS_CBP_MB,
BITS_COEFF_Y_MB,
BITS_COEFF_UV_MB,
BITS_DELTA_QUANT_MB,
MAX_BITCOUNTER_MB
} BitCountType;

typedef struct macroblock
{

int bitcounter[MAX_BITCOUNTER_MB];

} Macroblock;

In my clang plugin, the type of the field bitcounter is shown as ConstantArrayType of size 8.

Is there a way to determine that the size of this array is MAX_BITCOUNTER_MB and not 8, so I can make this structure dependent to the enum.

Thanks in advance.
Murat
|

Get the TypeSourceInfo (and, then, the TypeLoc) of the field. From there, you can find the actual size expression as written in the source code. It will be a DeclRefExpr pointing to the enumerator.

  • Doug

Thanks for the reply Doug!

I have following situation, I have a variable T of class Type which is the type of the array size expression.

My function progress following way.
ArrayType *AT = dyn_cast(T);
if (VariableArrayType::classof(AT)

else if (ConstantArrayType::classof(AT) {
ConstantArrayType *CAT = cast(AT);
}

For the structure field the type will be ConstantArrayType. Can you tell me how I can get TypeSourceInfo from it.

Thanks,
Murat

As I said before, you need to get the TypeSourceInfo from the field, i.e., the FieldDecl.

  • Doug

Thanks. I still have a problem.

I modified the field processing in this way:
void AAPConsumer::HandleRecordFields(const RecordDecl *RD)
{
Type *T;
for (RecordDecl::field_iterator I = RD->field_begin(),
E = RD->field_end(); I != E; I++) {
T = ((*I)->getType()).getTypePtr();
if (ArrayType::classof(T)) {
llvm::errs() << "Field: "; (*I)->dump(); T->dump();
llvm::errs() << " "; (*I)->dump();
(*I)->getTypeSourceInfo()->getTypeLoc().getTypePtr()->dump();
}
}
}

For the field bitcounter, I get following output:
Field: int bitcounter[8]: int identifier[8]
int bitcounter[8]: int identifier[8]

In both cases the array size is shown as 8 and the type is ConstantArrayType. I still don’t get size MAX_BITCOUNTER_MB. Am I doing something wrong?

Thanks,
Murat

You need to actually walk the structure returned by getTypeLoc(), not jump down immediately to the underlying type. ConstantArrayTypeLoc has the information you’re looking for.

  • Doug