elna/include/elna/boot/result.h

83 lines
2.0 KiB
C++

/* Miscellaneous types used across stage boundaries.
Copyright (C) 2025 Free Software Foundation, Inc.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#pragma once
#include <cstddef>
#include <string>
#include <deque>
#include <memory>
namespace elna::boot
{
/**
* Position in the source text.
*/
struct position
{
/// Line.
std::size_t line = 1;
/// Column.
std::size_t column = 1;
};
/**
* A compilation error consists of an error message and position.
*/
class error
{
protected:
error(const char *path, const struct position position);
public:
const struct position position;
const char *path;
virtual ~error() = default;
/// Error text.
virtual std::string what() const = 0;
/// Error line in the source text.
std::size_t line() const;
/// Error column in the source text.
std::size_t column() const;
};
class error_container
{
protected:
std::deque<std::unique_ptr<error>> m_errors;
error_container(const char *input_file);
public:
const char *input_file;
std::deque<std::unique_ptr<error>>& errors();
template<typename T, typename... Args>
void add_error(Args... arguments)
{
auto new_error = std::make_unique<T>(arguments...);
m_errors.emplace_back(std::move(new_error));
}
};
}