summaryrefslogtreecommitdiff
path: root/gcc/elna-diagnostic.cc
blob: 162d6cb99d5a2ff182a6740140934b5b246192b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Elna frontend specific diagnostic routines.
   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-diagnostic.h"
#include "elna/gcc/elna-tree.h"
#include "elna/gcc/elna1.h"

namespace elna::gcc
{
    linemap_guard::linemap_guard(const char *filename)
    {
        linemap_add(line_table, LC_ENTER, 0, filename, 1);
    }

    linemap_guard::~linemap_guard()
    {
        linemap_add(line_table, LC_LEAVE, 0, NULL, 0);
    }

    location_t get_location(const frontend::position *position)
    {
        linemap_line_start(line_table, position->line, 0);

        return linemap_position_for_column(line_table, position->column);
    }

    std::string print_aggregate_name(tree type, const std::string& kind_name)
    {
        if (TYPE_IDENTIFIER(type) == NULL_TREE)
        {
            return kind_name;
        }
        else
        {
            return std::string(IDENTIFIER_POINTER(TYPE_IDENTIFIER(type)));
        }
    }

    std::string print_type(tree type)
    {
        gcc_assert(TYPE_P(type));

        tree unqualified_type = get_qualified_type(type, TYPE_UNQUALIFIED);
        tree_code code = TREE_CODE(type);

        if (unqualified_type == elna_int_type_node)
        {
            return "Int";
        }
        else if (unqualified_type == elna_word_type_node)
        {
            return "Word";
        }
        else if (unqualified_type == elna_bool_type_node)
        {
            return "Bool";
        }
        else if (unqualified_type == elna_pointer_type_node)
        {
            return "Pointer";
        }
        else if (unqualified_type == elna_float_type_node)
        {
            return "Float";
        }
        else if (unqualified_type == elna_char_type_node)
        {
            return "Char";
        }
        else if (unqualified_type == elna_string_type_node)
        {
            return "String";
        }
        else if (is_void_type(unqualified_type)) // For procedures without a return type.
        {
            return "()";
        }
        else if (POINTER_TYPE_P(unqualified_type))
        {
            tree pointer_target_type = TREE_TYPE(type);

            if (TREE_CODE(pointer_target_type) == FUNCTION_TYPE)
            {
                return print_type(pointer_target_type);
            }
            else
            {
                return std::string("^" + print_type(pointer_target_type));
            }
        }
        else if (code == FUNCTION_TYPE)
        {
            std::string output = "proc(";
            tree parameter_type = TYPE_ARG_TYPES(type);
            while (TREE_VALUE(parameter_type) != void_type_node)
            {
                output += print_type(TREE_VALUE(parameter_type));
                parameter_type = TREE_CHAIN(parameter_type);
                if (TREE_VALUE(parameter_type) == void_type_node)
                {
                    break;
                }
                else
                {
                    output += ", ";
                }
            }
            output += ')';
            tree return_type = TREE_TYPE(type);

            if (!is_void_type(return_type))
            {
                output += " -> " + print_type(return_type);
            }
            return output;
        }
        else if (code == ARRAY_TYPE)
        {
            return "array";
        }
        else if (code == RECORD_TYPE)
        {
            return print_aggregate_name(unqualified_type, "record");
        }
        else if (code == UNION_TYPE)
        {
            return print_aggregate_name(unqualified_type, "union");
        }
        else if (code == ENUMERAL_TYPE)
        {
            return print_aggregate_name(unqualified_type, "enumeration");
        }
        else
        {
            return "<<unknown-type>>";
        }
        gcc_unreachable();
    }

    void report_errors(const std::deque<std::unique_ptr<frontend::error>>& errors)
    {
        for (const auto& error : errors)
        {
            location_t gcc_location{ UNKNOWN_LOCATION };

            if (error->position.line != 0 || error->position.column != 0)
            {
                gcc_location = elna::gcc::get_location(&error->position);
            }
            error_at(gcc_location, error->what().c_str());
        }
    }
}