Any way how to instantiate templates even when it is not necessary for the compilation?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Any way how to instantiate templates even when it is not necessary for the compilation?

natris
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.
 
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
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Any way how to instantiate templates even when it is not necessary for the compilation?

Nathan Jeffords
How would clang know what type to instantiate it with? Would it make sense to replace T with object, or create a CSharp generic instead in you translation process?

On Fri, May 4, 2012 at 8:43 AM, <[hidden email]> 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.
 
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
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Any way how to instantiate templates even when it is not necessary for the compilation?

Nathan Jeffords
In reply to this post by natris
Is the original definition of the template available in the XML file, not just the reference to the specific instantiations (i.e.  IMyEnumerator<int>).

On Mon, May 7, 2012 at 12:08 PM, Ondrej Kolacek <[hidden email]> wrote:
(another try from different mail account; I hope I was able to clarify
my issue better. )

Hello,
I am working on a tool which takes c++ header files of a C++ DLL 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 <typename T>
IMyEnumerator {
virtual HRESULT GetCurrent(T*item) = 0;
};
class IWhateverInterface {
virtual IMyEnumerator<int> & GetEnumerator() = 0;
};
And here lies the problem - since the template IMyEnumerator<int> is
only used in this declaration, this template specialization is not
instantiated and the XML does not contain full CXXRecord of the class.
I however need to know the full structure of the type because I need
to generate the glue layer for the enumerator.

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).

NOTE: one example of code which forces the instantiation is
class Dummy : public IMyEnumerator<int>
{
};

Thank you for any idea,
Ondrej


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Any way how to instantiate templates even when it is not necessary for the compilation?

Nathan Jeffords
My gut tells me that if it isn't already available, then it will be difficult as the instantiation has to occur for the final compilation, but that must be occurring in a later phase of compilation than the one where the XML file is generated. Their is also a good chance that the instantiation depends on information that isn't available until after the XML file is generated (things like overload candidates found during argument dependent look-up). I am not very familiar with clang's code or structure so this is just speculation, perhaps a query on clangs developers list might give you more insight into what options you may have.

On Tue, May 8, 2012 at 3:22 PM, Ondrej Kolacek <[hidden email]> wrote:
Hello,
I do not have the code right here but if I understood your question
correctly, you are asking whether the "generic" structures of the
template are dumped into xml while the "specialization" is printed
incomplete. Yes, xml dump of the structure containing stuff like "this
template has parameter typename T" is present. But I do not dare to
use the dump of the template's generic structure for doing the
template parameter substitution within my code as this looks
nontrivial, very error prone, and not all the necessary information
for the substitution is currently available in XML.

I currently plan to try to somehow hack the parser to call the
function which instantiates templates from some convenient place,
although whether I will be able to find such a place and the correct
function I am not certain. This is why I have tried to find some
alternate solution via this mail.

Ondrej


On Tue, May 8, 2012 at 1:03 AM, Nathan Jeffords <[hidden email]> wrote:
> Is the original definition of the template available in the XML file, not
> just the reference to the specific instantiations (i.e.
> IMyEnumerator<int>).
>
>
> On Mon, May 7, 2012 at 12:08 PM, Ondrej Kolacek <[hidden email]> wrote:
>>
>> (another try from different mail account; I hope I was able to clarify
>> my issue better. )
>>
>> Hello,
>> I am working on a tool which takes c++ header files of a C++ DLL 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 <typename T>
>> IMyEnumerator {
>> virtual HRESULT GetCurrent(T*item) = 0;
>> };
>> class IWhateverInterface {
>> virtual IMyEnumerator<int> & GetEnumerator() = 0;
>> };
>> And here lies the problem - since the template IMyEnumerator<int> is
>> only used in this declaration, this template specialization is not
>> instantiated and the XML does not contain full CXXRecord of the class.
>> I however need to know the full structure of the type because I need
>> to generate the glue layer for the enumerator.
>>
>> 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).
>>
>> NOTE: one example of code which forces the instantiation is
>> class Dummy : public IMyEnumerator<int>
>> {
>> };
>>
>> Thank you for any idea,
>> Ondrej
>
>


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev