<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Oct 3, 2015 at 12:30 AM, Georg Bartels via cfe-users <span dir="ltr"><<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear Clang Users,<br>
<br>
I have a particular programming problem, and several discussion forums suggested Clang as a possible solution. Being totally new to Clang and its community, is this the right place to ask questions about how to use Clang for a particular programming task? If not, where should I turn to?<br>
<br>
In case this is the right forum for such questions, I continue describing my problem. Basically, I want to compile a class which inherits from a virtual interface class at runtime of my program. The program should then create an instance of this new class and use it through its interface.<br>
<br>
Let me quickly sketch an example. The static version of my program would look like this:<br>
<br>
class Base<br>
{<br>
  public:<br>
    virtual int calc() const;<br>
};<br>
<br>
class Derived: public Base<br>
{<br>
  public:<br>
    virtual int calc() const<br>
    {<br>
      return 42;<br>
    }<br>
};<br>
<br>
int main()<br>
{<br>
    std::cout << Derived().calc() << std::endl;<br>
    return 0;<br>
}<br>
<br>
<br>
The dynamic version (with two fuzzy lines of code) shall look like this:<br>
<br>
class Base<br>
{<br>
  public:<br>
    virtual void calc() const;<br>
};<br>
<br>
int main()<br>
{<br>
  std::string code = read_derived_code();<br>
<br>
  // two fuzzy lines for which I am looking for a solution<br>
  SomeCompiledObjectClass o = compile(code);<br>
  Base* d = SomeFactoryClass(o).createInstance();<br>
<br>
  std::cout << d->calc() << std::endl;<br>
  return 0;<br>
}<br>
<br>
Is Clang the right tool for solving my two fuzzy lines of code? If yes, which class should have a look at? If not, do you maybe know about another library/compile which can achieve this task?<br></blockquote><div><br></div><div>This migth be the sort of thing that would escalate to cfe-dev and/or llvm-dev.<br><br>What you're looking to do is JIT (Just In Time) compile some code. That involves using clang to turn the source code into LLVM IR and then LLVM to make machine code from that, put it in memory, make it executable, and call some part of it.<br><br>Your two fuzzy lines of code would look slightly different (chances are what you'd do is generate source code that included a private function that return an instance, then you would JIT compile that function, indirectly code generating anything in the class that was needed, etc).<br><br>I'm not sure where the best examples of the Clang half of this problem are. But for the second have, LLVM has the Kaleidoscope tutorials (especially the recently added ORC examples) that show how to use LLVM as a JIT compiler. You shuold be able to wrap up all these APIs into a fairly simple API for your one specific use case.<br><br>I think /maybe/ out there is an existing C++ rapid development environment using Clang (essentially a C++ interpreter/interactive command prompt like Python) that you might be able to grab the basics of "feed code to compiler, get IR" from.<br><br>- David</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Thanks a lot for your help!<br>
<br>
Cheers,<br>
Georg.<br>
<br>
_______________________________________________<br>
cfe-users mailing list<br>
<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users</a><br>
</blockquote></div><br></div></div>