Changing the size of ConstantArrayType

Hello,

i need a way to change the size of the ConstantArrayType after the AST
is created.
I thought about two ways to do it:
1. I'll add a setSize method to ConstantArrayType class.
or
2. I replace 'somehow' the node in the AST with a new one, which has
the size i need.

But i dont know what are the consequences when i'll do it the one or
the other way. And
is it possible to do it so anyway? What are the other options i have
to solve this problem?

Thx

i need a way to change the size of the ConstantArrayType after the AST
is created.

Types are immutable, and for good reason.

I assume that what you're actually trying to do is change the allocated
size of an array-typed variable or field.

I thought about two ways to do it:
1. I'll add a setSize method to ConstantArrayType class.

This would play havoc with the entire type system; don't do this.

or
2. I replace 'somehow' the node in the AST with a new one, which has
the size i need.

If my guess above is correct, then what you want to do is to set the type
of your VarDecl/FieldDecl to be the new type. It's not necessarily quite that
simple, though — you'll also need to modify the types of all the references
to that variable, and you'll need to pretend that all the ways in which the
length of a declared array is reflectively discoverable in code can't actually
happen (†).

John.

† The most obvious way to detect the length of an array is with template
argument deduction in C++; even in C, though, you have to worry about
code doing clever things with sizeof.

Hi Dimitrij,

Hello,

i need a way to change the size of the ConstantArrayType after the AST
is created.
I thought about two ways to do it:
1. I'll add a setSize method to ConstantArrayType class.

Types are immutable once created, so you can't do this.

or
2. I replace 'somehow' the node in the AST with a new one, which has
the size i need.

You can use ASTContext::getConstantArrayType to create a new
ConstantArrayType (or retrieve an existing one matching the given
parameters). But actually replacing the type everywhere in the AST
may be difficult, as the AST is not designed to be modified after
semantic analysis.

But i dont know what are the consequences when i'll do it the one or
the other way. And
is it possible to do it so anyway? What are the other options i have
to solve this problem?

To answer this question it would help if you explain more about what
you are trying to do and why you think you need to change array sizes
after AST construction.

Thanks,

Hello again,
thank you for your answers, John and Peter.

To answer this question it would help if you explain more about what
you are trying to do and why you think you need to change array sizes
after AST construction.

I'm going to try to explain my problem.

First of all, i have some class hierarchy, which could depend on some
template parameters:

template<class T> class Base { ... };
template<class T> class Derived1 {...};
template<class T> class Derived2 {...};

And another class:

template<class T> class Other {
  char buffer[ <here i need the max size of a derived class from Base> ];
};

I would name it something like the 'maxsizeof' operator of a class hierarchy.

Well i have another way to solve this problem, but i wanted to know if
it was possible
to solve it that way.

... as the AST is not designed to be modified after semantic analysis.

This is very sad and i realized it very late, otherwise i would have looked for
another tool. The AST is one of the things a lot of people want to
play with. Not
being able to modify or transform it, makes clang not very appealing.

Thanks

Hello again,
thank you for your answers, John and Peter.

To answer this question it would help if you explain more about what
you are trying to do and why you think you need to change array sizes
after AST construction.

I'm going to try to explain my problem.

First of all, i have some class hierarchy, which could depend on some
template parameters:

template<class T> class Base { ... };
template<class T> class Derived1 {...};
template<class T> class Derived2 {...};

And another class:

template<class T> class Other {
char buffer[ <here i need the max size of a derived class from Base> ];
};

I would name it something like the 'maxsizeof' operator of a class hierarchy.

That's actually implementable in Clang, but the problem you're trying to solve can't be solved in the general case. I could have one .cpp file with a Derived3 that is 1000 bytes long and another .cpp file with a Derived4 that's 2000 bytes. So Other's buffer would end up being 1000 bytes in one file and 2000 bytes in the other, and you'll crash when trying to pass Other from one translation unit to another.

... as the AST is not designed to be modified after semantic analysis.

This is very sad and i realized it very late, otherwise i would have looked for
another tool. The AST is one of the things a lot of people want to
play with. Not
being able to modify or transform it, makes clang not very appealing.

Changing the size of a single type in the program could end up causing the program to be compiled in a completely different way. It can't just be done as a local transformation; you potentially have to re-evaluate every class's layout , every template metaprogram, and so on. You need full semantic analysis to change the size of a member of a field, so it doesn't make sense to allow such a dangerous operation on the AST.

That said, Clang would benefit from better AST transformations. I don't know of anyone who is working on those.

  - Doug

Hi,

That's actually implementable in Clang, but the problem you're trying to solve can't be solved in the general case. I could have one .cpp file with
a Derived3 that is 1000 bytes long and another .cpp file with a Derived4 that's 2000 bytes. So Other's buffer would end up being 1000 bytes in
one file and 2000 bytes in the other, and you'll crash when trying to pass Other from one translation unit to another.

That's ok, because my programs consist of just one translation unit. I
already can calculate the max size of a class hierarchy.
Last thing that was missing is changing the size of an array. But
since this looks very difficult to do, i think i'm going to try
another
solution.

Thanks