tan  0.0.1
driver.h
1 #ifndef TAN_INCLUDE_COMPILER_H_
2 #define TAN_INCLUDE_COMPILER_H_
3 #include "base.h"
4 #include "tan/tan.h"
5 
6 namespace llvm {
7 class TargetMachine;
8 class Value;
9 } // namespace llvm
10 
11 namespace tanlang {
12 
13 class CodeGenerator;
14 class Program;
15 class TokenizedSourceFile;
16 class SourceFile;
17 class Package;
18 
19 /**
20  * \brief Compile a list of C++ and/or tan source files, and perform linking.
21  */
22 class CompilerDriver final {
23 public:
24  static CompilerDriver *instance() { return singleton; }
25 
26 private:
27  static inline CompilerDriver *singleton = nullptr;
28 
29 public:
30  /**
31  * \brief Import search directories
32  * FIXME: static variable?
33  */
34  static inline vector<str> import_dirs{};
35 
36  /**
37  * \brief Get a list of possible files that corresponds to an import. Check PACKAGES.md
38  * \param callee_path The path to the file which the import statement is in
39  * \param import_name The filename specified by the import statement
40  * \return A list of absolute paths to candidate files
41  */
42  static vector<str> resolve_package_import(const str &callee_path, const str &import_name);
43 
44 public:
45  CompilerDriver() = delete;
46  explicit CompilerDriver(TanCompilation config);
47  ~CompilerDriver();
48 
49  /**
50  * \brief Compile CXX or TAN source files and link their output object files
51  * \details The object files are named as "<name of the source file>.o" and they are located at current working
52  * directory.
53  * If current build is release, all exceptions are captured and `e.what()` is printed out to stderr.
54  * If current build is debug, all exceptions are not captured, making debugging easier.
55  *
56  * \param files The path to source files, can be relative or absolute path.
57  * They will be distinguished by their file extensions.
58  * \param config Compilation configuration, \see TanCompilation
59  */
60  void run(const vector<str> &files);
61 
62  /**
63  * \brief Parse the corresponding source file, and build AST
64  */
65  vector<Program *> parse(const vector<str> &files);
66 
67  void link(const vector<str> &input_paths);
68 
69  /**
70  * \brief Register a Package that has been spotted from source files, with top-level context stored inside.
71  */
72  void register_package(const str &name, Package *package);
73 
74  /**
75  * \brief Get a pointer to a Package. Semantic analysis is not guaranteed to be fully performed on it.
76  */
77  Package *get_package(const str &name);
78 
79  /**
80  * \brief Get a set of partially analyzed packages that can be used for cross-package dependency analysis.
81  * Note that some composite types need a full analysis for the dependent packages to work during codegen.
82  * \details Package dependencies are analyzed recursively but there's no guarantee that all of them are found.
83  * \note This function raises an error if it detects cyclic dependency.
84  * \param ps A list of Program's returned by the Parser
85  * \return A list of partially analyzed packages
86  */
87  vector<Package *> stage1_analysis(vector<Program *> ps);
88 
89 private:
90  /**
91  * \brief Compile TAN files and return a list of object files
92  */
93  vector<str> compile_tan(const vector<str> &files);
94 
95 private:
96  TanCompilation _config{};
97  llvm::TargetMachine *_target_machine = nullptr;
98 
99  umap<str, Package *> _packages{}; // including external dependencies, which are likely only partially analyzed
100 
101  enum class AnalyzeStatus : int {
102  None = 0, // umap default value relies on this
103  Processing,
104  Done,
105  };
106  umap<str, AnalyzeStatus> _package_status{}; // used to avoid recursive importing
107 };
108 
109 } // namespace tanlang
110 
111 #endif /* TAN_INCLUDE_COMPILER_H_ */
Compile a list of C++ and/or tan source files, and perform linking.
Definition: driver.h:22
vector< Package * > stage1_analysis(vector< Program * > ps)
Get a set of partially analyzed packages that can be used for cross-package dependency analysis....
Definition: driver.cpp:193
Package * get_package(const str &name)
Get a pointer to a Package. Semantic analysis is not guaranteed to be fully performed on it.
Definition: driver.cpp:183
static vector< str > import_dirs
Import search directories FIXME: static variable?
Definition: driver.h:34
void run(const vector< str > &files)
Compile CXX or TAN source files and link their output object files.
Definition: driver.cpp:146
vector< Program * > parse(const vector< str > &files)
Parse the corresponding source file, and build AST.
Definition: driver.cpp:313
static vector< str > resolve_package_import(const str &callee_path, const str &import_name)
Get a list of possible files that corresponds to an import. Check PACKAGES.md.
Definition: driver.cpp:342
void register_package(const str &name, Package *package)
Register a Package that has been spotted from source files, with top-level context stored inside.
Definition: driver.cpp:191
Compilation configuration.
Definition: tan.h:42