aboutsummaryrefslogtreecommitdiff
path: root/boot/semantic.cc
blob: 42b7b3c683d7fb3db5aa1c4c0565b733bf41fb37 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* Name analysis.
   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/boot/semantic.h"

#include <algorithm>
#include <set>

namespace elna::boot
{
    undeclared_error::undeclared_error(const std::string& identifier, const struct position position)
        : error(position), identifier(identifier)
    {
    }

    std::string undeclared_error::what() const
    {
        return "Type '" + identifier + "' not declared";
    }

    already_declared_error::already_declared_error(const std::string& identifier, const struct position position)
        : error(position), identifier(identifier)
    {
    }

    std::string already_declared_error::what() const
    {
        return "Symbol '" + identifier + "' has been already declared";
    }

    field_duplication_error::field_duplication_error(const std::string& field_name, const struct position position)
        : error(position), field_name(field_name)
    {
    }

    std::string field_duplication_error::what() const
    {
        return "Repeated field name '" + field_name + "'";
    }

    cyclic_declaration_error::cyclic_declaration_error(const std::vector<std::string>& cycle,
            const struct position position)
        : error(position), cycle(cycle)
    {
    }

    std::string cyclic_declaration_error::what() const
    {
        auto segment = std::cbegin(this->cycle);
        std::string message = "Type declaration forms a cycle: " + *segment;

        ++segment;
        for (; segment != std::cend(this->cycle); ++segment)
        {
            message += " -> " + *segment;
        }
        return message;
    }

    return_error::return_error(const std::string& identifier, const struct position position)
        : error(position), identifier(identifier)
    {
    }

    std::string return_error::what() const
    {
        return "Procedure '" + this->identifier + "' is expected to return, but does not have a return statement";
    }

    base_type_error::base_type_error(const std::string& base, const struct position position)
        : error(position), base(base)
    {
    }

    std::string base_type_error::what() const
    {
        return "Record base type '" + this->base + "' is not a record.";
    }

    type_analysis_visitor::type_analysis_visitor(symbol_bag bag)
        : error_container(), bag(bag)
    {
    }

    void type_analysis_visitor::visit(procedure_declaration *declaration)
    {
        this->returns = false;
        walking_visitor::visit(declaration);

        if (declaration->body.has_value())
        {
            if (!this->returns && declaration->heading().return_type.proper_type != nullptr)
            {
                add_error<return_error>(declaration->identifier.name, declaration->position());
            }
        }
    }

    void type_analysis_visitor::visit(return_statement *statement)
    {
        walking_visitor::visit(statement);
        this->returns = true;
    }

    bool type_analysis_visitor::check_unresolved_symbol(std::shared_ptr<alias_type> alias,
            std::vector<std::string>& alias_path)
    {
        if (std::find(std::cbegin(alias_path), std::cend(alias_path), alias->name) != std::cend(alias_path))
        {
            return false;
        }
        alias_path.push_back(alias->name);

        if (auto another_alias = alias->reference.get<alias_type>())
        {
            return check_unresolved_symbol(another_alias, alias_path);
        }
        return true;
    }

    void type_analysis_visitor::visit(type_declaration *declaration)
    {
        std::vector<std::string> alias_path;
        auto unresolved_type = this->bag.lookup(declaration->identifier.name)->is_type()->symbol.get<alias_type>();

        if (!check_unresolved_symbol(unresolved_type, alias_path))
        {
            add_error<cyclic_declaration_error>(alias_path, declaration->position());
        }
        else
        {
            walking_visitor::visit(declaration);
        }
    }

    void type_analysis_visitor::visit(record_type_expression *expression)
    {
        if (expression->base.has_value())
        {
            type base_type = inner_aliased_type(this->bag.lookup(expression->base.value())->is_type()->symbol);
            if (base_type.get<record_type>() == nullptr)
            {
                add_error<base_type_error>(expression->base.value(), expression->position());
            }
        }
        walking_visitor::visit(expression);
    }

    name_analysis_visitor::name_analysis_visitor(symbol_bag bag)
        : error_container(), bag(bag)
    {
    }

    std::pair<procedure_type, std::vector<std::string>> name_analysis_visitor::build_procedure(
            procedure_type_expression& expression)
    {
        procedure_type::return_t result_return;

        if (expression.return_type.no_return)
        {
            result_return = procedure_type::return_t(std::monostate{});
        }
        else if (expression.return_type.proper_type != nullptr)
        {
            expression.return_type.proper_type->accept(this);
            result_return = procedure_type::return_t(this->current_type);
        }
        else
        {
            result_return = procedure_type::return_t();
        }
        std::pair<procedure_type, std::vector<std::string>> result_type{
            procedure_type(result_return), std::vector<std::string>()
        };
        for (auto& [parameter_names, parameters_type] : expression.parameters)
        {
            parameters_type->accept(this);
            for (auto& parameter_name : parameter_names)
            {
                result_type.first.parameters.push_back(this->current_type);
                result_type.second.push_back(parameter_name);
            }
        }
        return result_type;
    }

    void name_analysis_visitor::visit(type_declaration *declaration)
    {
        walking_visitor::visit(declaration);
        auto resolved = this->bag.resolve(declaration->identifier.name, this->current_type);
        auto info = std::make_shared<type_info>(type(resolved));

        info->exported = declaration->identifier.exported;
        this->bag.enter(declaration->identifier.name, info);
    }

    void name_analysis_visitor::visit(pointer_type_expression *expression)
    {
        walking_visitor::visit(expression);
        this->current_type = type(std::make_shared<pointer_type>(this->current_type));
    }

    void name_analysis_visitor::visit(array_type_expression *expression)
    {
        walking_visitor::visit(expression);
        auto result_type = std::make_shared<array_type>(this->current_type, expression->size);

        this->current_type = type(result_type);
    }

    std::vector<type_field> name_analysis_visitor::build_composite_type(const std::vector<field_declaration>& fields)
    {
        std::vector<type_field> result;
        std::set<std::string> field_names;

        for (auto& field : fields)
        {
            field.second->accept(this);
            for (auto& field_name : field.first)
            {
                if (field_names.find(field_name) != field_names.cend())
                {
                    add_error<field_duplication_error>(field_name, field.second->position());
                }
                else
                {
                    field_names.insert(field_name);
                    result.push_back(std::make_pair(field_name, this->current_type));
                }
            }
        }
        return result;
    }

    void name_analysis_visitor::visit(record_type_expression *expression)
    {
        std::shared_ptr<record_type> result_type;
        if (expression->base.has_value())
        {
            if (auto unresolved_alias = this->bag.declared(expression->base.value()))
            {
                result_type = std::make_shared<record_type>(type(unresolved_alias));
            }
            else if (auto base_symbol = this->bag.lookup(expression->base.value()))
            {
                if (auto base_type_info = base_symbol->is_type())
                {
                    result_type = std::make_shared<record_type>(base_type_info->symbol);
                }
                else
                {
                    add_error<base_type_error>(expression->base.value(), expression->position());
                    this->current_type = type();
                    return;
                }
            }
            else
            {
                add_error<undeclared_error>(expression->base.value(), expression->position());
                this->current_type = type();
                return;
            }
        }
        else
        {
            result_type = std::make_shared<record_type>();
        }
        result_type->fields = build_composite_type(expression->fields);

        this->current_type = type(result_type);
    }

    void name_analysis_visitor::visit(union_type_expression *expression)
    {
        auto result_type = std::make_shared<union_type>();

        result_type->fields = build_composite_type(expression->fields);

        this->current_type = type(result_type);
    }

    void name_analysis_visitor::visit(procedure_type_expression *expression)
    {
        std::shared_ptr<procedure_type> result_type =
            std::make_shared<procedure_type>(std::move(build_procedure(*expression).first));

        this->current_type = type(result_type);
    }

    void name_analysis_visitor::visit(enumeration_type_expression *expression)
    {
        std::shared_ptr<enumeration_type> result_type = std::make_shared<enumeration_type>(expression->members);

        this->current_type = type(result_type);
    }

    std::shared_ptr<variable_info> name_analysis_visitor::register_variable(const std::string& name,
            const bool is_extern, const struct position position)
    {
        auto variable_symbol = std::make_shared<variable_info>(this->current_type, is_extern);

        if (!this->bag.enter(name, variable_symbol))
        {
            add_error<already_declared_error>(name, position);
        }
        return variable_symbol;
    }

    void name_analysis_visitor::visit(variable_declaration *declaration)
    {
        walking_visitor::visit(declaration);

        for (const identifier_definition& variable_identifier : declaration->identifiers)
        {
            auto variable_symbol = register_variable(variable_identifier.name, declaration->is_extern,
                    declaration->position());
            variable_symbol->exported = variable_identifier.exported;
        }
    }

    void name_analysis_visitor::visit(constant_declaration *declaration)
    {
        walking_visitor::visit(declaration);
        auto constant_symbol = std::make_shared<constant_info>(this->current_literal);

        constant_symbol->exported = declaration->identifier.exported;
        this->bag.enter(declaration->identifier.name, constant_symbol);
    }

    void name_analysis_visitor::visit(procedure_declaration *declaration)
    {
        std::shared_ptr<procedure_info> info;
        auto [heading, parameter_names] = build_procedure(declaration->heading());

        if (declaration->body.has_value())
        {
            info = std::make_shared<procedure_info>(heading, std::move(parameter_names), this->bag.enter());
            auto name_iterator = std::cbegin(info->names);
            auto type_iterator = std::cbegin(heading.parameters);

            while (name_iterator != std::cend(info->names) && type_iterator != std::cend(heading.parameters))
            {
                this->current_type = *type_iterator;
                auto variable_symbol = register_variable(*name_iterator, false, declaration->heading().position());
                variable_symbol->exported = false;

                ++name_iterator;
                ++type_iterator;
            }
            for (constant_declaration *const constant : declaration->body.value().constants())
            {
                constant->accept(this);
            }
            for (variable_declaration *const variable : declaration->body.value().variables())
            {
                variable->accept(this);
            }
            for (statement *const statement : declaration->body.value().statements())
            {
                statement->accept(this);
            }
            this->bag.leave();
        }
        else
        {
            info = std::make_shared<procedure_info>(heading, std::move(parameter_names));
        }
        info->exported = declaration->identifier.exported;
        this->bag.enter(declaration->identifier.name, info);
    }

    void name_analysis_visitor::visit(procedure_call *call)
    {
        auto name_expression = call->callable().is_named();

        if (name_expression == nullptr || name_expression->name != "assert")
        {
            call->callable().accept(this);
        }
        for (expression *const argument : call->arguments)
        {
            argument->accept(this);
        }
    }

    void name_analysis_visitor::visit(unit *unit)
    {
        for (type_declaration *const type : unit->types)
        {
            type->accept(this);
        }
        for (variable_declaration *const variable : unit->variables)
        {
            variable->accept(this);
        }
        for (constant_declaration *const constant : unit->constants)
        {
            constant->accept(this);
        }
        for (procedure_declaration *const procedure : unit->procedures)
        {
            procedure->accept(this);
        }
        if (unit->entry_point.has_value())
        {
            this->bag.enter();
            auto variable_type = this->bag.lookup("Int")->is_type()->symbol;
            this->bag.enter("count", std::make_shared<variable_info>(variable_type, false));

            variable_type = this->bag.lookup("Char")->is_type()->symbol;
            variable_type = type(std::make_shared<pointer_type>(variable_type));
            variable_type = type(std::make_shared<pointer_type>(variable_type));
            this->bag.enter("parameters", std::make_shared<variable_info>(variable_type, false));

            for (statement *const statement : unit->entry_point.value())
            {
                statement->accept(this);
            }
            this->bag.leave();
        }
    }

    void name_analysis_visitor::visit(traits_expression *trait)
    {
        if (!trait->parameters.empty())
        {
            trait->parameters.front()->accept(this);
            trait->types.push_back(this->current_type);
        }
    }

    void name_analysis_visitor::visit(cast_expression *expression)
    {
        walking_visitor::visit(expression);
        expression->expression_type = this->current_type;
    }

    void name_analysis_visitor::visit(named_expression *expression)
    {
        if (auto unresolved_alias = this->bag.declared(expression->name))
        {
            this->current_type = type(unresolved_alias);
        }
        else if (auto from_symbol_table = this->bag.lookup(expression->name))
        {
            if (auto type_symbol = from_symbol_table->is_type())
            {
                this->current_type = type_symbol->symbol;
            }
        }
        else
        {
            add_error<undeclared_error>(expression->name, expression->position());
            this->current_type = type();
        }
    }

    void name_analysis_visitor::visit(literal<std::int32_t> *literal)
    {
        this->current_literal = literal->value;
    }

    void name_analysis_visitor::visit(literal<std::uint32_t> *literal)
    {
        this->current_literal = literal->value;
    }

    void name_analysis_visitor::visit(literal<double> *literal)
    {
        this->current_literal = literal->value;
    }

    void name_analysis_visitor::visit(literal<bool> *literal)
    {
        this->current_literal = literal->value;
    }

    void name_analysis_visitor::visit(literal<unsigned char> *literal)
    {
        this->current_literal = literal->value;
    }

    void name_analysis_visitor::visit(literal<std::nullptr_t> *literal)
    {
        this->current_literal = literal->value;
    }

    void name_analysis_visitor::visit(literal<std::string> *literal)
    {
        this->current_literal = literal->value;
    }

    declaration_visitor::declaration_visitor()
        : error_container()
    {
    }

    void declaration_visitor::visit(import_declaration *)
    {
    }

    void declaration_visitor::visit(unit *unit)
    {
        for (import_declaration *const _import : unit->imports)
        {
            _import->accept(this);
        }
        for (type_declaration *const type : unit->types)
        {
            type->accept(this);
        }
    }

    void declaration_visitor::visit(type_declaration *declaration)
    {
        const std::string& type_identifier = declaration->identifier.name;

        if (!this->unresolved.insert({ type_identifier, std::make_shared<alias_type>(type_identifier) }).second)
        {
            add_error<already_declared_error>(declaration->identifier.name, declaration->position());
        }
    }
}