Once I have an AST, how do I compile it?

I've managed to create a test program, included below for the curious, which creates a super simple AST corresponding to "void foo() { }". What do I call to generate code from it? Any pointers on what classes/files to look at?

Best,
Martin

#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/TextDiagnosticBuffer.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/AST/ASTContext.h"
#include "clang/Parse/Scope.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/LLVMContext.h"
#include "llvm/Target/TargetSelect.h"

using namespace clang;

using namespace std;

int main(int argc, char **argv) {
   CompilerInstance MyCI;

   MyCI.setLLVMContext(new llvm::LLVMContext);

   llvm::InitializeAllTargets();
   llvm::InitializeAllAsmPrinters();

   TextDiagnosticBuffer DiagsBuffer;
   Diagnostic Diags(&DiagsBuffer);
   CompilerInvocation::CreateFromArgs(MyCI.getInvocation(), NULL, NULL,
                                      Diags);

   // Create the actual diagnostics engine.
   MyCI.createDiagnostics(0, NULL);
   if (!MyCI.hasDiagnostics())
     return 1;

   MyCI.createSourceManager();
   MyCI.createFileManager();

   // Create the target instance.
   MyCI.setTarget(TargetInfo::CreateTargetInfo(MyCI.getDiagnostics(), MyCI.getTargetOpts()));

   MyCI.createPreprocessor();
   MyCI.getPreprocessor();

   MyCI.createASTContext();

   // ************ Initialization finished. Now time for the fun.

   ASTContext &context = MyCI.getASTContext();
   DeclContext *DC = context.getTranslationUnitDecl();

   IdentifierInfo &info = MyCI.getPreprocessor().getIdentifierTable().get("foo");
   DeclarationName myname(&info);

   FunctionDecl *fd = FunctionDecl::Create(context, DC, SourceLocation(), myname,
    context.DoubleTy, NULL, FunctionDecl::None,
    /* isInline = */ false, /* hasPrototype = */ false);

}

Call CreateLLVMCodeGen in include/clang/CodeGen/ModuleBuilder.h to get
an ASTConsumer, then call the appropriate APIs (see
include/clang/AST/ASTConsumer.h) to make it generate code.

-Eli

Thanks Eli. What's the memory contract? Should I call delete on CreateLLVMCodeGen()'s value when I'm done with it?

Best,
Martin

Yes, delete to clean up (or use an llvm::OwningPtr).

-Eli