667 lines
18 KiB
C++

/* Abstract syntax tree representation.
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 <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "elna/boot/result.h"
namespace elna
{
namespace boot
{
enum class binary_operator
{
sum,
subtraction,
multiplication,
division,
remainder,
equals,
not_equals,
less,
greater,
less_equal,
greater_equal,
disjunction,
conjunction,
exclusive_disjunction,
shift_left,
shift_right
};
enum class unary_operator
{
reference,
negation,
minus
};
class variable_declaration;
class constant_definition;
class procedure_definition;
class type_definition;
class procedure_call;
class cast_expression;
class assign_statement;
class if_statement;
class while_statement;
class traits_expression;
class block;
class program;
class binary_expression;
class unary_expression;
class basic_type;
class array_type;
class pointer_type;
class record_type;
class union_type;
class procedure_type;
class variable_expression;
class array_access_expression;
class field_access_expression;
class dereference_expression;
template<typename T>
class number_literal;
/**
* Interface for AST visitors.
*/
struct parser_visitor
{
virtual void visit(variable_declaration *) = 0;
virtual void visit(constant_definition *) = 0;
virtual void visit(procedure_definition *) = 0;
virtual void visit(type_definition *) = 0;
virtual void visit(procedure_call *) = 0;
virtual void visit(cast_expression *) = 0;
virtual void visit(traits_expression *) = 0;
virtual void visit(assign_statement *) = 0;
virtual void visit(if_statement *) = 0;
virtual void visit(while_statement *) = 0;
virtual void visit(block *) = 0;
virtual void visit(program *) = 0;
virtual void visit(binary_expression *) = 0;
virtual void visit(unary_expression *) = 0;
virtual void visit(basic_type *) = 0;
virtual void visit(array_type *) = 0;
virtual void visit(pointer_type *) = 0;
virtual void visit(record_type *) = 0;
virtual void visit(union_type *) = 0;
virtual void visit(procedure_type *) = 0;
virtual void visit(variable_expression *) = 0;
virtual void visit(array_access_expression *) = 0;
virtual void visit(field_access_expression *is_field_access) = 0;
virtual void visit(dereference_expression *is_dereference) = 0;
virtual void visit(number_literal<std::int32_t> *) = 0;
virtual void visit(number_literal<std::uint32_t> *) = 0;
virtual void visit(number_literal<double> *) = 0;
virtual void visit(number_literal<bool> *) = 0;
virtual void visit(number_literal<unsigned char> *) = 0;
virtual void visit(number_literal<std::nullptr_t> *) = 0;
virtual void visit(number_literal<std::string> *) = 0;
};
/**
* A visitor which visits all nodes but does nothing.
*/
struct empty_visitor : parser_visitor
{
virtual void visit(variable_declaration *) override;
virtual void visit(constant_definition *definition) override;
virtual void visit(procedure_definition *definition) override;
virtual void visit(type_definition *definition) override;
virtual void visit(traits_expression *trait) override;
virtual void visit(procedure_call *call) override;
virtual void visit(cast_expression *expression) override;
virtual void visit(assign_statement *statement) override;
virtual void visit(if_statement *) override;
virtual void visit(while_statement *) override;
virtual void visit(block *block) override;
virtual void visit(program *program) override;
virtual void visit(binary_expression *expression) override;
virtual void visit(unary_expression *expression) override;
virtual void visit(basic_type *) override;
virtual void visit(array_type *expression) override;
virtual void visit(pointer_type *) override;
virtual void visit(record_type *expression) override;
virtual void visit(union_type *expression) override;
virtual void visit(procedure_type *expression) override;
virtual void visit(variable_expression *) override;
virtual void visit(array_access_expression *expression) override;
virtual void visit(field_access_expression *expression) override;
virtual void visit(dereference_expression *expression) override;
virtual void visit(number_literal<std::int32_t> *) override;
virtual void visit(number_literal<std::uint32_t> *) override;
virtual void visit(number_literal<double> *) override;
virtual void visit(number_literal<bool> *) override;
virtual void visit(number_literal<unsigned char> *) override;
virtual void visit(number_literal<std::nullptr_t> *) override;
virtual void visit(number_literal<std::string> *) override;
};
/**
* AST node.
*/
class node
{
const struct position source_position;
protected:
/**
* \param position Source code position.
*/
explicit node(const position position);
public:
virtual ~node() = default;
virtual void accept(parser_visitor *) = 0;
/**
* \return Node position in the source code.
*/
const struct position& position() const;
};
class statement : public virtual node
{
protected:
statement();
};
class expression : public virtual node
{
protected:
expression();
};
/**
* Symbol definition.
*/
class definition : public node
{
protected:
definition(const struct position position, const std::string& identifier, const bool exported);
public:
const std::string identifier;
const bool exported;
};
/**
* Some type expression.
*/
class top_type : public node, public std::enable_shared_from_this<top_type>
{
protected:
top_type(const struct position position);
};
/**
* Expression defining a basic type.
*/
class basic_type : public top_type
{
const std::string m_name;
public:
/**
* \param position Source code position.
* \param name Type name.
*/
basic_type(const struct position position, const std::string& name);
virtual void accept(parser_visitor *visitor) override;
const std::string& base_name();
};
class array_type : public top_type
{
std::shared_ptr<top_type> m_base;
public:
const std::uint32_t size;
array_type(const struct position position, std::shared_ptr<top_type> base, const std::uint32_t size);
virtual void accept(parser_visitor *visitor) override;
top_type& base();
};
class pointer_type : public top_type
{
std::shared_ptr<top_type> m_base;
public:
pointer_type(const struct position position, std::shared_ptr<top_type> base);
virtual void accept(parser_visitor *visitor) override;
top_type& base();
};
using field_t = std::pair<std::string, std::shared_ptr<top_type>>;
using fields_t = std::vector<field_t>;
class record_type : public top_type
{
public:
fields_t fields;
record_type(const struct position position, fields_t&& fields);
virtual void accept(parser_visitor *visitor) override;
};
class union_type : public top_type
{
public:
fields_t fields;
union_type(const struct position position, fields_t&& fields);
virtual void accept(parser_visitor *visitor) override;
};
/**
* Variable declaration.
*/
class variable_declaration : public definition
{
std::shared_ptr<top_type> m_type;
public:
variable_declaration(const struct position position, const std::string& identifier,
std::shared_ptr<top_type> type, const bool exported = false);
virtual void accept(parser_visitor *visitor) override;
top_type& variable_type();
};
/**
* Literal expression.
*/
class literal : public expression
{
protected:
literal();
};
/**
* Constant definition.
*/
class constant_definition : public definition
{
literal *m_body;
public:
/**
* \param position Source code position.
* \param identifier Constant name.
* \param body Constant value.
*/
constant_definition(const struct position position, const std::string& identifier,
const bool exported, literal *body);
virtual void accept(parser_visitor *visitor) override;
literal& body();
virtual ~constant_definition() override;
};
/**
* Tags a procedure type as never returning.
*/
struct no_return_t
{
};
constexpr no_return_t no_return{};
/**
* Procedure type.
*/
class procedure_type : public top_type
{
public:
const std::shared_ptr<top_type> return_type;
const bool no_return;
std::vector<variable_declaration *> parameters;
procedure_type(const struct position position, std::shared_ptr<top_type> return_type = nullptr);
procedure_type(const struct position position, no_return_t);
virtual void accept(parser_visitor *visitor) override;
virtual ~procedure_type() override;
};
/**
* Procedure definition.
*/
class procedure_definition : public definition
{
std::shared_ptr<procedure_type> m_heading;
public:
block *const body;
expression *const returning;
procedure_definition(const struct position position, const std::string& identifier,
const bool exported, std::shared_ptr<procedure_type> heading,
block *const body = nullptr, expression *const returning = nullptr);
virtual void accept(parser_visitor *visitor) override;
procedure_type& heading();
virtual ~procedure_definition() override;
};
/**
* Type definition.
*/
class type_definition : public definition
{
std::shared_ptr<top_type> m_body;
public:
type_definition(const struct position position, const std::string& identifier,
const bool exported, std::shared_ptr<top_type> expression);
virtual void accept(parser_visitor *visitor) override;
top_type& body();
};
/**
* Cast expression.
*/
class cast_expression : public expression
{
std::shared_ptr<top_type> m_target;
expression *m_value;
public:
cast_expression(const struct position position, std::shared_ptr<top_type> target, expression *value);
virtual void accept(parser_visitor *visitor) override;
top_type& target();
expression& value();
virtual ~cast_expression() override;
};
class traits_expression : public expression
{
std::shared_ptr<top_type> m_type;
public:
const std::string name;
traits_expression(const struct position position, const std::string& name, std::shared_ptr<top_type> type);
virtual void accept(parser_visitor *visitor) override;
top_type& type();
};
/**
* List of statements paired with a condition.
*/
class conditional_statements
{
expression *m_prerequisite;
public:
std::vector<statement *> statements;
conditional_statements(expression *prerequisite);
expression& prerequisite();
virtual ~conditional_statements();
};
class designator_expression : public expression
{
public:
virtual variable_expression *is_variable();
virtual array_access_expression *is_array_access();
virtual field_access_expression *is_field_access();
virtual dereference_expression *is_dereference();
protected:
designator_expression();
};
class variable_expression : public designator_expression
{
public:
const std::string name;
variable_expression(const struct position position, const std::string& name);
virtual void accept(parser_visitor *visitor) override;
variable_expression *is_variable() override;
};
class array_access_expression : public designator_expression
{
expression *m_base;
expression *m_index;
public:
array_access_expression(const struct position position, expression *base, expression *index);
virtual void accept(parser_visitor *visitor) override;
expression& base();
expression& index();
array_access_expression *is_array_access() override;
~array_access_expression() override;
};
class field_access_expression : public designator_expression
{
expression *m_base;
std::string m_field;
public:
field_access_expression(const struct position position, expression *base,
const std::string& field);
virtual void accept(parser_visitor *visitor) override;
expression& base();
std::string& field();
field_access_expression *is_field_access() override;
~field_access_expression() override;
};
class dereference_expression : public designator_expression
{
expression *m_base;
public:
dereference_expression(const struct position position, expression *base);
virtual void accept(parser_visitor *visitor) override;
expression& base();
dereference_expression *is_dereference() override;
~dereference_expression() override;
};
/**
* Procedure call expression.
*/
class procedure_call : public expression, public statement
{
designator_expression *m_callable;
public:
std::vector<expression *> arguments;
procedure_call(const struct position position, designator_expression *callable);
virtual void accept(parser_visitor *visitor) override;
designator_expression& callable();
virtual ~procedure_call() override;
};
class assign_statement : public statement
{
designator_expression *m_lvalue;
expression *m_rvalue;
public:
/**
* \param position Source code position.
* \param lvalue Left-hand side.
* \param rvalue Assigned expression.
*/
assign_statement(const struct position position, designator_expression *lvalue,
expression *rvalue);
virtual void accept(parser_visitor *visitor) override;
designator_expression& lvalue();
expression& rvalue();
virtual ~assign_statement() override;
};
/**
* If-statement.
*/
class if_statement : public statement
{
conditional_statements *m_body;
std::vector<statement *> *m_alternative;
public:
std::vector<conditional_statements *> branches;
if_statement(const struct position position, conditional_statements *body,
std::vector<statement *> *alternative = nullptr);
virtual void accept(parser_visitor *visitor) override;
conditional_statements& body();
std::vector<statement *> *alternative();
virtual ~if_statement() override;
};
/**
* While-statement.
*/
class while_statement : public statement
{
conditional_statements *m_body;
public:
std::vector<conditional_statements *> branches;
while_statement(const struct position position, conditional_statements *body);
virtual void accept(parser_visitor *visitor) override;
conditional_statements& body();
virtual ~while_statement() override;
};
class block : public node
{
public:
std::vector<variable_declaration *> variables;
std::vector<constant_definition *> constants;
std::vector<statement *> body;
block(const struct position position);
virtual void accept(parser_visitor *visitor) override;
virtual ~block() override;
};
class program : public block
{
public:
std::vector<type_definition *> types;
std::vector<procedure_definition *> procedures;
program(const struct position position);
virtual void accept(parser_visitor *visitor) override;
virtual ~program() override;
};
template<typename T>
class number_literal : public literal
{
public:
T value;
number_literal(const struct position position, const T& value)
: node(position), value(value)
{
}
virtual void accept(parser_visitor *visitor) override
{
visitor->visit(this);
}
};
class binary_expression : public expression
{
expression *m_lhs;
expression *m_rhs;
binary_operator m_operator;
public:
binary_expression(const struct position position, expression *lhs,
expression *rhs, const binary_operator operation);
virtual void accept(parser_visitor *visitor) override;
expression& lhs();
expression& rhs();
binary_operator operation() const;
virtual ~binary_expression() override;
};
class unary_expression : public expression
{
expression *m_operand;
unary_operator m_operator;
public:
unary_expression(const struct position position, expression *operand,
const unary_operator operation);
virtual void accept(parser_visitor *visitor) override;
expression& operand();
unary_operator operation() const;
virtual ~unary_expression() override;
};
const char *print_binary_operator(const binary_operator operation);
}
}