blog-static/code/compiler/13/error.hpp

50 lines
1.4 KiB
C++
Raw Normal View History

2020-09-08 18:38:05 -07:00
#pragma once
#include <exception>
2020-09-09 15:08:43 -07:00
#include <optional>
2020-09-08 18:38:05 -07:00
#include "type.hpp"
2020-09-09 15:08:43 -07:00
#include "location.hh"
#include "parse_driver.hpp"
using maybe_location = std::optional<yy::location>;
2020-09-08 18:38:05 -07:00
2020-09-17 18:30:41 -07:00
class compiler_error : std::exception {
private:
std::string description;
maybe_location loc;
2020-09-11 21:29:49 -07:00
2020-09-17 18:30:41 -07:00
public:
compiler_error(std::string d, maybe_location l = std::nullopt)
: description(std::move(d)), loc(std::move(l)) {}
2020-09-11 21:29:49 -07:00
2020-09-17 18:30:41 -07:00
const char* what() const noexcept override;
2020-09-11 21:29:49 -07:00
2020-09-17 18:30:41 -07:00
void print_about(std::ostream& to);
void print_location(std::ostream& to, file_mgr& fm, bool highlight = false);
2020-09-11 21:29:49 -07:00
2020-09-17 18:30:41 -07:00
void pretty_print(std::ostream& to, file_mgr& fm);
2020-09-11 21:29:49 -07:00
};
2020-09-17 18:30:41 -07:00
class type_error : compiler_error {
private:
2020-09-08 18:38:05 -07:00
2020-09-17 18:30:41 -07:00
public:
type_error(std::string d, maybe_location l = std::nullopt)
: compiler_error(std::move(d), std::move(l)) {}
2020-09-08 18:38:05 -07:00
2020-09-17 18:30:41 -07:00
const char* what() const noexcept override;
void pretty_print(std::ostream& to, file_mgr& fm);
2020-09-08 18:38:05 -07:00
};
2020-09-17 18:30:41 -07:00
class unification_error : public type_error {
private:
type_ptr left;
type_ptr right;
2020-09-08 18:38:05 -07:00
2020-09-17 18:30:41 -07:00
public:
unification_error(type_ptr l, type_ptr r, maybe_location loc = std::nullopt)
: left(std::move(l)), right(std::move(r)),
2020-09-09 15:08:43 -07:00
type_error("failed to unify types", std::move(loc)) {}
2020-09-17 18:30:41 -07:00
void pretty_print(std::ostream& to, file_mgr& fm, type_mgr& mgr);
2020-09-08 18:38:05 -07:00
};