[cfe-dev] Creating New Types

Chris Lattner clattner at apple.com
Tue Aug 7 10:36:37 PDT 2007


On Aug 6, 2007, at 11:14 PM, Bill Wendling wrote:
> Hi all,
> I'd like to create new types based on previous types. E.g., if I have
> two types like this:
>
> 	int const * volatile * const * const
> 	float     * volatile * const * volatile * const * const * restrict
>
> I'd like to be able to create these two types from them:
>
> 	char const    * volatile * const *
> 	char volatile * const    * const *

> What's the best way to code this? I'm currently thinking of using the
> context to get the builtin type "char", and then creating a QualType
> around that. Then continuing to create QualTypes around that, etc.

The answer is that you have to recursively build the type from the  
inside out.  To get the first, do something like this:

QualType T = ASTContext.CharTy;   // char
T.addConst();                     // const char

T = ASTContext.getPointerType(T); // const char *
T.addVolatile();                  // const char * volatile
...

> When I do this, will the QualType be uniqued? Is there something not
> good about doing this? Or is there a simpler way of doing this?

Types are always autouniqued.

> (This is related to the description of "casting away constness" in
> the C++ standard [5.2.11p8].)

How so?  const_cast should never change the underlying type (e.g.  
from float to char).  In fact, my understanding of const cast is that  
it isn't a "recursive" process, it just looks at the top-level  
qualifiers and pointer/reference.

-Chris



More information about the cfe-dev mailing list