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.

type_env.hpp 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #pragma once
  2. #include <map>
  3. #include <string>
  4. #include <set>
  5. #include <optional>
  6. #include "graph.hpp"
  7. #include "type.hpp"
  8. struct type_env;
  9. using type_env_ptr = std::shared_ptr<type_env>;
  10. enum class visibility { global,local };
  11. class type_env {
  12. private:
  13. struct variable_data {
  14. type_scheme_ptr type;
  15. visibility vis;
  16. std::optional<std::string> mangled_name;
  17. variable_data()
  18. : variable_data(nullptr, visibility::local, std::nullopt) {}
  19. variable_data(type_scheme_ptr t, visibility v, std::optional<std::string> n)
  20. : type(std::move(t)), vis(v), mangled_name(std::move(n)) {}
  21. };
  22. type_env_ptr parent;
  23. std::map<std::string, variable_data> names;
  24. std::map<std::string, type_ptr> type_names;
  25. public:
  26. type_env(type_env_ptr p) : parent(std::move(p)) {}
  27. type_env() : type_env(nullptr) {}
  28. void find_free(const type_mgr& mgr, std::set<std::string>& into) const;
  29. void find_free_except(const type_mgr& mgr, const group& avoid,
  30. std::set<std::string>& into) const;
  31. type_scheme_ptr lookup(const std::string& name) const;
  32. bool is_global(const std::string& name) const;
  33. void set_mangled_name(const std::string& name, const std::string& mangled);
  34. const std::string& get_mangled_name(const std::string& name) const;
  35. type_ptr lookup_type(const std::string& name) const;
  36. void bind(const std::string& name, type_ptr t,
  37. visibility v = visibility::local);
  38. void bind(const std::string& name, type_scheme_ptr t,
  39. visibility v = visibility::local);
  40. void bind_type(const std::string& type_name, type_ptr t);
  41. void generalize(const std::string& name, const group& grp, type_mgr& mgr);
  42. };
  43. type_env_ptr type_scope(type_env_ptr parent);