aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/parsemod.c
blob: bda027df4fe540daf75426f1e9e7e1d248b57410 (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

/*
 * Parsing of per-module files.
 *
 * Copyright © 2025-2026 Samuel Lidén Borell <samuel@kodafritt.se>
 *
 * SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later
 */
#include "compiler.h"
#include "token.h"
#include <assert.h>
#include <string.h>

unsigned num_sources = 0;
char *(sources[MAX_SOURCES]) = { 0 };

static struct TreeNode *dependencies = NULL;
struct DependencyInfo *dependencies_list = NULL;


void parse_source_index(FILE *f)
{
    char line[SOURCELINE_MAX];
    size_t len;
    while (read_source_line(f, line, &len, STRIP_COMMENTS)) {
        if (num_sources == MAX_SOURCES) {
            FAIL("Too many sources for bootstrap compiler.");
        }
        check_filename(line, "slul");
        if (len == 14 && !memcmp(line, "interface.slul", 14)) {
            is_library = true;
        } else if (len == 15 && !memcmp(line, "interfaces.slul", 15)) {
            error("Interface file should be called interface.slul "
                  "(remove the 's')");
        } else if ((len>=15 && !memcmp(line+len-15, "/interface.slul", 15)) ||
                   (len>=16 && !memcmp(line+len-16, "/interfaces.slul", 16))) {
            error("interface.slul is only supported in the root directory");
        } else {
            sources[num_sources++] = dupmemz(line, len);
        }
    }
}

static const char *next_word(const char *last_start, size_t *prev_len_out)
{
    const char *s = last_start;
    bool spaces = false;
    assert(*s && *s != ' ' && *s != '\t');
    while (*s && *s != ' ' && *s != '\t') s++;
    *prev_len_out = (unsigned)(s - last_start);
    /* Skip whitespace after */
    while (*s == ' ' || *s == '\t') { s++; spaces = true; }
    if (!*s) {
        if (spaces) {
            warning("Trailing whitespace");
        }
        return NULL;
    }
    return s;
}

static void check_modname(const char *name, size_t len)
{
    if (len > 50) {
        error_len("Module name of dependency too long", name, len);
    }
    /* TODO */
    /* XXX require that the first letter is lowercase? or all letters? */
}

static void check_version(const char *version, size_t len)
{
    if (len > 50) {
        error_len("Version of dependency too long", version, len);
    }
    /* TODO */
}

static void add_dependency(const char *apihash,
                           const char *modname, size_t modname_len,
                           const char *apiversion, size_t apiversion_len)
{
    struct DependencyInfo *dep;

    assert(modname != NULL);
    check_modname(modname, modname_len);
    if (apiversion) {
        assert(apihash != NULL);
        check_version(apiversion, apiversion_len);
    }

    dep = (struct DependencyInfo *)tree_insert_str(&dependencies,
            modname, modname_len, NULL, sizeof(struct DependencyInfo));
    NO_NULL(dep);
    dep->next = dependencies_list;
    dependencies_list = dep;
    if (dep->node.is_new) {
        dep->use_latest_api = false;
        dep->apiversions = NULL;
        dep->apiversions_list = NULL;
    } else if (dep->use_latest_api && !apihash) {
        error("Duplicate module dependency");
    } else if ((dep->apiversions && !apihash) ||
               (!dep->apiversions && apihash)) {
        /* This is only a warning, in order to allow adding a unversioned
           dependency during development. */
        warning("Mixing dependencies of the same module "
                "with and without API hashes.");
    }

    if (!apihash) {
        dep->use_latest_api = true;
    } else {
        unsigned char binhash[32]; /* copied by tree_insert/create_node */
        struct DepApiVersion *ver;

        if (!unbase64url(apihash, 43, binhash, 32)) {
            error("Invalid encoding of API hash (must be Base64url)");
        }

        ver = (struct DepApiVersion *)tree_insert_str(&dep->apiversions,
                (const char*)binhash, 32,
                NULL, sizeof(struct DepApiVersion));
        NO_NULL(ver);
        if (!ver->node.is_new) {
            error("Duplicate API version");
        }
        ver->apiversion = apiversion ?
                dupmemz(apiversion, apiversion_len) : NULL;
        ver->apiversion_len = apiversion_len;
        ver->next = dep->apiversions_list;
        dep->apiversions_list = ver;
    }
}

static void parse_dependency_line(const char *line)
{
    const char *word1, *word2;
    const char *apihash, *modname, *apiversion;
    size_t len1;
    size_t modname_len, apiversion_len;

    if (!line[0]) {
        return; /* Empty line */
    } else if (line[0] == ' ' || line[0] == '\t') {
        error("Leading whitespace in dependencies file");
    }

    word1 = line;
    word2 = next_word(word1, &len1);
    assert(len1 > 0);
    if (word2) {
        const char *junk;
        /* Full line with <api-hash> <module-name> [<api-version>] */
        apihash = word1;
        if (len1 != 43) {
            error_len("Invalid length of API hash of dependency, "
                      "should be 43 characters (256 bits)",
                      apihash, len1);
        }
        modname = word2;
        apiversion = next_word(word2, &modname_len);
        if (apiversion) {
            junk = next_word(apiversion, &apiversion_len);
            if (junk != NULL) {
                error_str("Unexpected stuff at end of dependency line", junk);
            }
        } else {
            apiversion_len = 0;
        }
    } else {
        /* Short line with just a module name.
           This is for prototyping and internal submodules only. */
        modname = word1;
        modname_len = len1;
        apihash = NULL;
        apiversion = NULL;
        apiversion_len = 0;
    }

    add_dependency(apihash,
                   modname, modname_len,
                   apiversion, apiversion_len);
}

void parse_dependencies_list(FILE *f)
{
    char line[SOURCELINE_MAX];
    size_t len;
    while (read_source_line(f, line, &len, STRIP_COMMENTS)) {
        parse_dependency_line(line);
    }
}