6#include <clang/AST/ASTConcept.h>
7#include <clang/Sema/SemaConcept.h>
8#include <clang/Frontend/TextDiagnosticPrinter.h>
9#include <clang/CodeGen/CodeGenAction.h>
10#include <clang/Frontend/CompilerInstance.h>
12#include <clang/AST/Mangle.h>
13#include <clang/ASTMatchers/ASTMatchFinder.h>
14#include <clang/ASTMatchers/ASTMatchers.h>
16#include <llvm-c/Core.h>
17#include <llvm-c/Support.h>
18#include <llvm-c/TargetMachine.h>
19#include <llvm-c/ExecutionEngine.h>
20#include <llvm-c/Analysis.h>
22#include <llvm/Support/InitLLVM.h>
23#include <llvm/Support/TargetSelect.h>
25#include <llvm/InitializePasses.h>
26#include <llvm/Passes/PassBuilder.h>
27#include <llvm/ExecutionEngine/Orc/LLJIT.h>
29#include <llvm/IR/LegacyPassManagers.h>
30#include <llvm/Transforms/Scalar.h>
34#include <llvm/Bitcode/BitcodeWriter.h>
35#include <llvm/LinkAllIR.h>
36#include <llvm/LinkAllPasses.h>
38#include "clang/Driver/Driver.h"
39#include "clang/Driver/Compilation.h"
41#include "llvm-c/Core.h"
42#include "llvm-c/Target.h"
43#include "llvm-c/TargetMachine.h"
45#include "llvm/IR/LegacyPassManager.h"
46#include "llvm/MC/TargetRegistry.h"
47#include "llvm/Support/CodeGen.h"
49#include "llvm/Support/InitLLVM.h"
51#include "llvm/Target/TargetMachine.h"
53#include "clang/Frontend/CompilerInstance.h"
54#include "clang/CodeGen/CodeGenAction.h"
69#define NEWLANG_INDENT_OP " "
70#define NEWLANG_PREFIX "newlang"
71#define NEWLANG_NS "newlang"
76 std::string
MangaledFuncCPP(
const char *name,
const char *space =
nullptr);
97 std::vector<std::shared_ptr<Context>>
m_ctx;
105 return *(*
m_ctx.begin());
108 clang::IntrusiveRefCntPtr<clang::DiagnosticOptions>
DiagOpts;
110 clang::IntrusiveRefCntPtr<clang::DiagnosticIDs>
pDiagIDs;
185 if (LLVMInitializeNativeTarget() || LLVMInitializeNativeAsmParser() || LLVMInitializeNativeAsmPrinter()) {
192 LLVMInitializeAllTargetInfos();
193 LLVMInitializeAllTargets();
194 LLVMInitializeAllTargetMCs();
195 LLVMInitializeAllAsmParsers();
196 LLVMInitializeAllAsmPrinters();
209 ObjPtr Run(
const std::string_view str,
Obj* args =
nullptr);
218 static bool ModuleCreate(
FileModule &data,
const std::string_view module_name,
const TermPtr &include,
const std::string_view source, llvm::Module *bc =
nullptr);
223 std::string
MakeCodeRepl(
const std::string_view source,
const std::string_view name);
227 std::unique_ptr<llvm::Module>
MakeLLVMModule(std::string_view source,
const std::vector<std::string> opts, std::string temp_dir =
"");
228 bool MakeObjFile(
const std::string_view filename, llvm::Module &module,
const std::vector<std::string> opts);
229 bool LinkObjToExec(
const std::string_view filename, std::vector<std::string> objs, std::vector<std::string> opts = {});
241 bool MakeCppExec(
const std::string_view source,
const std::string_view filename, std::vector<std::string> opts = {});
245 static std::string
MakeMain(
const std::vector<std::string> &include);
246 static std::string
MakeMainEmbed(
const std::string_view embed_source,
const std::vector<std::string> &include);
303 std::string
GetIndent(int64_t offset = 0);
365 static bool Execute(
const char *exec, std::string *out =
nullptr,
int *exit_code =
nullptr);
366 static bool GccMakeModule(
const char * file,
const char * module,
const char * opts =
nullptr, std::string *out =
nullptr,
int *exit_code =
nullptr);
400 out <<
"#include \"pch.h\"\n";
401 out <<
"#include \"newlang.h\"\n";
402 out <<
"#include \"builtin.h\"\n";
469 return ci.
GetIndent(-1) +
"return nullptr; // default return from function\n";
485 return ci.
GetIndent() +
"return nullptr; // default return from function\n";
static ObjPtr GetIndexField(Context *ctx, ObjPtr obj, TermPtr term, bool create_field=false)
static void SelectTerms_(TermPtr &obj, std::vector< TermPtr > &terms)
static std::string BeginIterators(CompileInfo &ci, TermPtr args, std::string &output, std::vector< std::string > &iters)
std::string WriteFunctionOp(CompileInfo &ci, TermPtr &op, FunctionStep step)
static std::string MakeCommentLine(std::string comment)
static bool StringTemplate(std::string &format, Context *ctx)
static std::string WriteArgsForCall_(CompileInfo &ci, TermPtr &func)
static bool WriteFunctionName_(TermPtr &func, std::ostream &out, bool is_transparent=false)
static std::string MakeSequenceOpsCpp(CompileInfo &ci, TermPtr elem, bool top_level)
static bool Execute(const char *exec, std::string *out=nullptr, int *exit_code=nullptr)
static void MakeCppFileCallTerm(CompileInfo &ci, TermPtr ast, std::ostream &out, size_t deep)
static bool GccMakeModule(const char *file, const char *module, const char *opts=nullptr, std::string *out=nullptr, int *exit_code=nullptr)
static bool MakeFunctionCpp(CompileInfo &ci, std::string func_name, TermPtr &func_define, std::ostream &out)
static std::string MakeCppFileVariable(CompileInfo &ci, TermPtr &args, std::ostream &out)
static void WriteDeclarationFunctions_(CompileInfo &ci, TermPtr &func, std::ostream &out, std::vector< std::string > &func_list)
static bool CompileModule(const char *filename, const char *output)
static void ReplaceFuncArgs(TermPtr &func_define, std::string &str)
static std::string WriteFunctionCheckOp_(CompileInfo &ci, TermPtr &op, const char *check_true, const char *check_false)
static std::string GetImpl(CompileInfo &ci, TermPtr term, std::string &output)
static std::string WriteFunctionBodyOTHER_(CompileInfo &ci, TermPtr &op, FunctionStep step=FunctionStep::OPERATION)
static std::string WriteFunctionBodyCLEAN_(CompileInfo &ci, TermPtr &op, FunctionStep step=FunctionStep::OPERATION)
static std::string WriteSimpleBody_(CompileInfo &ci, TermPtr &func)
static void WriteIncludeFiles_(std::ostream &out)
static std::string MakeCppFileCallArgs(CompileInfo &ci, TermPtr &args, TermPtr proto)
static std::string MakeFunctionBodyCpp(CompileInfo &ci, TermPtr ast)
static void MakeCppFileConstants(CompileInfo &ci, TermPtr elem, std::ostream &out)
static bool ExecFunc(FunctionType *func, Obj *def_arg, Context *ctx, Obj &in, ObjPtr &out)
static void ReplaceSourceVariable(CompileInfo &ci, size_t count, std::string &body)
static std::string EncodeNonAsciiCharacters(const char *text)
static void MakeCppFileFunctions(CompileInfo &ci, TermPtr elem, std::ostream &out)
static bool MakeCppFile(TermPtr list, std::ostream &out, const char *source=nullptr, Context *ctx=nullptr)
static std::string MakeIteratorCallArgs_(CompileInfo &ci, TermPtr args, std::vector< std::string > &iters)
StorageTerm::ModuleMapType m_external
static std::string ExtractStaticVars(const TermPtr &ast, const std::string_view module)
static std::string ReplaceObjectInEmbedSource(const std::string_view embed, std::vector< std::string > &include, const TermPtr args=nullptr)
std::string MakeCodeFunction(const TermPtr &term)
static std::string MakeCommentPlace(const TermPtr &term)
static std::string MakeMain(const std::vector< std::string > &include)
bool LinkObjToExec(const std::string_view filename, std::vector< std::string > objs, std::vector< std::string > opts={})
std::vector< std::string > m_includes
JIT(const StringArray &args={})
std::string MakeCodeModule(const TermPtr &term, std::string_view name, bool is_main)
clang::IntrusiveRefCntPtr< clang::DiagnosticIDs > pDiagIDs
void MakeFunctionRecursive_(const TermPtr &term, std::string &output, const std::string_view module)
ObjPtr Run(const std::string_view str, Obj *args=nullptr)
static bool LLVMInitializeJIT()
std::vector< ModulePtr > m_module
clang::IntrusiveRefCntPtr< clang::DiagnosticOptions > DiagOpts
clang::TextDiagnosticPrinter * textDiagPrinter
static JIT * Instance(const StringArray &args={})
static JIT * ReCreate(const StringArray &args={})
static std::string RegExpInlineComment(const std::string_view src)
std::unique_ptr< llvm::Module > MakeLLVMModule(std::string_view source, const std::vector< std::string > opts, std::string temp_dir="")
static std::string ExtractFunctionDecls(const TermPtr &ast, const std::string_view module)
const JIT & operator=(const JIT &)=delete
std::vector< std::shared_ptr< Context > > m_ctx
ObjPtr RunFile(std::string file, Obj *args=nullptr)
clang::DiagnosticsEngine * pDiagnosticsEngine
std::string MakeApplicationSource(const TermPtr &ast)
static std::string MakeFunctionPrototype(const TermPtr &func, const std::string_view module)
bool MakeObjFile(const std::string_view filename, llvm::Module &module, const std::vector< std::string > opts)
static std::string MakeMainEmbed(const std::string_view embed_source, const std::vector< std::string > &include)
static bool LLVMInitialize()
static TermPtr MainArgs()
std::string MakeFuncDeclarations_(const TermPtr &term)
std::string MakeCodeRepl(const std::string_view source, const std::string_view name)
static std::string MakeBodyFunction(const TermPtr &ast)
bool ModuleCreate(FileModule &data, const std::string_view source)
bool MakeCppExec(const std::string_view source, const std::string_view filename, std::vector< std::string > opts={})
bool CreateMacro(const std::string_view text)
std::map< std::string, StorageTerm > ModuleMapType
#define LOG_RUNTIME(format,...)
#define ASSERT(condition)
std::shared_ptr< Parser > ParserPtr
std::string MangaledFuncCPP(const char *name, const char *space=nullptr)
std::shared_ptr< Term > TermPtr
std::shared_ptr< Obj > ObjPtr
std::shared_ptr< RunTime > RuntimePtr
std::vector< std::string > StringArray
bool Tranliterate(const wchar_t c, std::wstring &str)
std::string MangaledFunc(const std::string name)
std::shared_ptr< const Term > TermPtrConst
std::shared_ptr< Macro > MacroPtr
std::string MangleName(const char *name)
ObjPtr FunctionType(Context *ctx, Obj &in)
CompileInfo(RuntimePtr rt)
TermPtr isFunction(TermPtr term)
TermPtr findObject(std::string name)
TermPtr isArgument(TermPtr term)
TermPtr isVariable(TermPtr term)
std::set< std::string > iterators
bool isLocalAccess(TermPtr term)
std::string GetIndent(int64_t offset=0)