[cfe-dev] [LLVMdev] Any way how to instantiate templates even when it is not necessary for the compilation?

David Blaikie dblaikie at gmail.com
Fri May 4 09:28:07 PDT 2012

[moving to cfe-dev, the clang development mailing list]

On Fri, May 4, 2012 at 8:43 AM,  <natris at centrum.cz> wrote:
> Hello,
> I am working on a tool which takes c++ header files and based on interfaces defined in them generates csharp classes and glue layer for interop. For analyzing the c++ code I am currently using clang, which parses the headers and writes output into xml via -ast-print-xml (yes, this unfortunately means that it is a very old version of clang). I then use these xml files to generate the csharp code. Since the header files are written in consistent style, it works pretty well.
> But new version of headers contains templates. Nothing fancy, usually stuff like:
> template
> IMyEnumerator {
> virtual HRESULT GetCurrent(T*item) = 0;
> };
> class IWhateverInterface {
> virtual IMyEnumerator & GetEnumerator() = 0;
> };
> And here lies the problem - since the template IMyEnumerator does not have to be instantiated for clang to compile the headers, this Specialization is not instantiated and the XML does not contain full CXXRecord of the class. I however need to know the full structure because I need to generate the glue layer for the enumerator.

What template parameters would you use to instantiate IMyEnumerator?
(& your code is a little invalid - since IWhateverInterface doesn't
specify any template arguments for the IMyEnumerator return type - if
it had, then the type would've been instantiated)

Different argument lists produce different types - and even with a
specialization of the template it won't create specializations of any
members that aren't referenced - so you'd still end up with a weird
hybrid thing you'd have to special case anyway, most likely. (it's
possible that there are class templates that can't have all their
members instantiated for any given type (eg: for one type member
function A is valid and B is invalid, and for another type the inverse
is true))

- David

> It seems to me that this would be quite a common problem for similar tools and someone might have stumbled upon it already, and because of this I'd like to ask: is there any clean way how to force instantiation of templates that do not strictly have to be instantiated? Because if it isn't, this means that I will either have to somehow analyze the headers and generate code which forces the instantiation (which is not really trivial) or try to hack the code somehow to force the instantiation (probably possible but certainly not pretty).
> Thank you,
> Ondrej
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

More information about the cfe-dev mailing list