/* Builtin definitions.
   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/>.  */

#include "elna/gcc/elna-builtins.h"
#include "elna/gcc/elna1.h"
#include "stor-layout.h"
#include "stringpool.h"
#include "elna/gcc/elna-tree.h"

namespace elna::gcc
{
    void init_ttree()
    {
        elna_int_type_node = long_integer_type_node;
        elna_word_type_node = size_type_node;
        elna_char_type_node = unsigned_char_type_node;
        elna_byte_type_node = make_unsigned_type(8);
        elna_float_type_node = double_type_node;

        elna_bool_type_node = boolean_type_node;
        elna_bool_true_node = boolean_true_node;
        elna_bool_false_node = boolean_false_node;

        elna_pointer_nil_node = null_pointer_node;

        elna_string_type_node = make_node(RECORD_TYPE);
        tree string_ptr_type = build_pointer_type_for_mode(elna_char_type_node, VOIDmode, true);

        elna_string_length_field_node = build_field(UNKNOWN_LOCATION,
                elna_string_type_node, "length", build_qualified_type(elna_word_type_node, TYPE_QUAL_CONST));
        elna_string_ptr_field_node = build_field(UNKNOWN_LOCATION,
                elna_string_type_node, "ptr", build_qualified_type(string_ptr_type, TYPE_QUAL_CONST));

        TYPE_FIELDS(elna_string_type_node) = chainon(elna_string_ptr_field_node, elna_string_length_field_node);
        layout_type(elna_string_type_node);
    }

    static
    void declare_builtin_type(std::shared_ptr<symbol_table> symbol_table, const char *name, tree type)
    {
        tree identifier = get_identifier(name);
        tree type_declaration = build_decl(UNKNOWN_LOCATION, TYPE_DECL, identifier, type);

        TREE_PUBLIC(type_declaration) = 1;
        TYPE_NAME(type_declaration) = identifier;

        symbol_table->enter(name, type_declaration);
    }

    std::shared_ptr<symbol_table> builtin_symbol_table()
    {
        std::shared_ptr<elna::gcc::symbol_table> symbol_table = std::make_shared<elna::gcc::symbol_table>();

        declare_builtin_type(symbol_table, "Int", elna_int_type_node);
        declare_builtin_type(symbol_table, "Word", elna_word_type_node);
        declare_builtin_type(symbol_table, "Char", elna_char_type_node);
        declare_builtin_type(symbol_table, "Bool", elna_bool_type_node);
        declare_builtin_type(symbol_table, "Byte", elna_byte_type_node);
        declare_builtin_type(symbol_table, "Float", elna_float_type_node);
        declare_builtin_type(symbol_table, "String", elna_string_type_node);

        return symbol_table;
    }
}