Compile time string to type parsing


I would like to add a feature to Clang that evaluates a string and returns a type (at compile time). For example using V2 = __parse_type("struct { float x, y; };").
I had a look at how Clang parses decltype and __underlying_type but I have had no luck getting it to work yet.

Could anyone share some suggestions for getting this to work? Also how costly would this be from a maintenance point of view? Is the AST code frequently modified?

Thank you very much

I suspect it may be somewhat involved, but the way I would approach this is to take the string literal contents from the operand, form a Lexer from that content and use that Lexer to parse the tokens to the end of the fake stream before restoring the regular lexer. This way, you’ll reuse the existing parser logic (Parser::ParseTypeName()) to form the type.

1 Like