A Hugo incarnation of the blog.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

definition.hpp 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include <memory>
  3. #include <vector>
  4. #include <set>
  5. #include "instruction.hpp"
  6. #include "llvm_context.hpp"
  7. #include "parsed_type.hpp"
  8. #include "type_env.hpp"
  9. struct ast;
  10. using ast_ptr = std::unique_ptr<ast>;
  11. struct constructor {
  12. std::string name;
  13. std::vector<parsed_type_ptr> types;
  14. int8_t tag;
  15. constructor(std::string n, std::vector<parsed_type_ptr> ts)
  16. : name(std::move(n)), types(std::move(ts)) {}
  17. };
  18. using constructor_ptr = std::unique_ptr<constructor>;
  19. struct definition_defn {
  20. std::string name;
  21. std::vector<std::string> params;
  22. ast_ptr body;
  23. type_env_ptr env;
  24. type_env_ptr var_env;
  25. std::set<std::string> free_variables;
  26. type_ptr full_type;
  27. type_ptr return_type;
  28. std::vector<instruction_ptr> instructions;
  29. llvm::Function* generated_function;
  30. definition_defn(std::string n, std::vector<std::string> p, ast_ptr b)
  31. : name(std::move(n)), params(std::move(p)), body(std::move(b)) {
  32. }
  33. void find_free(type_mgr& mgr, type_env_ptr& env);
  34. void insert_types(type_mgr& mgr);
  35. void typecheck(type_mgr& mgr);
  36. void compile();
  37. void declare_llvm(llvm_context& ctx);
  38. void generate_llvm(llvm_context& ctx);
  39. };
  40. using definition_defn_ptr = std::unique_ptr<definition_defn>;
  41. struct definition_data {
  42. std::string name;
  43. std::vector<std::string> vars;
  44. std::vector<constructor_ptr> constructors;
  45. type_env_ptr env;
  46. definition_data(
  47. std::string n,
  48. std::vector<std::string> vs,
  49. std::vector<constructor_ptr> cs)
  50. : name(std::move(n)), vars(std::move(vs)), constructors(std::move(cs)) {}
  51. void insert_types(type_env_ptr& env);
  52. void insert_constructors() const;
  53. void generate_llvm(llvm_context& ctx);
  54. };
  55. using definition_data_ptr = std::unique_ptr<definition_data>;