[cfe-dev] Changing the size of ConstantArrayType

Douglas Gregor dgregor at apple.com
Mon Jan 31 16:01:19 PST 2011


On Jan 31, 2011, at 3:37 PM, Dimitrij Kotrev wrote:

> Hello again,
> thank you for your answers, John and Peter.
> 
> 2011/1/31 Peter Collingbourne <peter at pcc.me.uk>:
>> 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






More information about the cfe-dev mailing list