aboutsummaryrefslogtreecommitdiff
path: root/boot/dependency.cc
blob: 7e103aa0552d5214792180c122825625f57bc652 (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
/* Dependency graph 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/dependency.h"

#include "elna/boot/driver.h"
#include "elna/boot/materialization.h"
#include "elna/boot/name_analysis.h"
#include "elna/boot/type_check.h"
#include "elna/boot/validation.h"
#include "parser.hh"

namespace elna::boot
{
    circular_import_error::circular_import_error(const source_position position,
            const std::string& module_name)
        : diagnostic(position), module_name(module_name)
    {
    }

    std::string circular_import_error::what() const
    {
        return "Circular import of module '" + this->module_name + "'";
    }

    read_result read_source(std::istream& entry_point, const target_info& target)
    {
        driver parse_driver;
        lexer tokenizer(entry_point);
        yy::parser parser(tokenizer, parse_driver);

        if (parser() != 0)
        {
            diagnostic_list errors;
            std::swap(errors, parse_driver.errors());
            return read_result{ std::in_place_type<diagnostic_list>, std::move(errors) };
        }
        std::unique_ptr<unit> tree;
        std::swap(tree, parse_driver.tree);
        materialization_visitor materialization_visitor(target);
        tree->accept(&materialization_visitor);

        if (materialization_visitor.has_errors())
        {
            diagnostic_list errors;
            std::swap(errors, materialization_visitor.errors());
            return read_result{ std::in_place_type<diagnostic_list>, std::move(errors) };
        }
        return read_result{ std::in_place_type<std::unique_ptr<unit>>, std::move(tree) };
    }

    analysis_result analyze_semantics(std::unique_ptr<unit>& tree,
            const std::vector<std::shared_ptr<symbol_table>>& imports,
            const std::shared_ptr<symbol_table>& globals, const target_info& target,
            const std::filesystem::path& module_path)
    {
        declaration_visitor declarations{};
        tree->accept(&declarations);
        analysis_result result{ .value = { std::move(declarations.unresolved), globals }, .errors = {} };

        if (declarations.has_errors())
        {
            std::swap(result.errors, declarations.errors());
            return result;
        }
        for (const auto& import : imports)
        {
            result.value.add_import(import);
        }
        name_analysis_visitor name_analyser(result.value, target, module_path);
        tree->accept(&name_analyser);

        if (name_analyser.has_errors())
        {
            std::swap(result.errors, name_analyser.errors());
            return result;
        }
        type_analysis_visitor type_analyzer(result.value, target);
        tree->accept(&type_analyzer);

        if (type_analyzer.has_errors())
        {
            std::swap(result.errors, type_analyzer.errors());
            return result;
        }
        validation_visitor validator(result.value, target);
        tree->accept(&validator);

        if (validator.has_errors())
        {
            std::swap(result.errors, validator.errors());
            return result;
        }
        return result;
    }

    std::filesystem::path build_path(const std::vector<std::string>& segments)
    {
        std::filesystem::path result;
        std::vector<std::string>::const_iterator segment_iterator = std::cbegin(segments);

        if (segment_iterator == std::cend(segments))
        {
            return result;
        }
        result = *segment_iterator;

        ++segment_iterator;
        for (; segment_iterator != std::cend(segments); ++segment_iterator)
        {
            result /= *segment_iterator;
        }
        result.replace_extension(".elna");

        return result;
    }
}