aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/cc/parser.cxx
blob: 5e6f43a0b513cfa1535cd1824982cd89968ff80d (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
// file      : libbuild2/cc/parser.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/cc/parser.hxx>

#include <libbuild2/cc/lexer.hxx>

using namespace std;
using namespace butl;

namespace build2
{
  namespace cc
  {
    using type = token_type;

    unit parser::
    parse (ifdstream& is, const path_name& in)
    {
      lexer l (is, in);
      l_ = &l;

      unit u;
      u_ = &u;

      // If the source has errors then we want the compiler to issues the
      // diagnostics. However, the errors could as likely be because we are
      // mis-parsing things. Initially, as a middle ground, we were going to
      // issue warnings. But the problem with this approach is that they are
      // easy to miss. So for now we fail. And it turns out we don't mis-
      // parse much.

      // We keep a {}-balance and skip everything at depth 1 and greater.
      // While after P1703 and P1857 everything that we are interested in
      // (module and import declarations) are treated as pseudo-pp-directives
      // and recognized everywhere, they are illegal everywhere execept at
      // depth 0. So we are going to skip for performance reasons and expect
      // the compiler to complain about the syntax rather than, say, module
      // BMI not being found.
      //
      int64_t bb (0);

      token t;
      for (bool n (true); (n ? l_->next (t) : t.type) != type::eos; )
      {
        // Break to stop, continue to continue, and set n to false if the
        // next token is already extracted.
        //
        n = true;

        switch (t.type)
        {
        case type::lcbrace:
          {
            ++bb;
            continue;
          }
        case type::rcbrace:
          {
            if (--bb < 0)
              break; // Imbalance.

            continue;
          }
        case type::identifier:
          {
            // Constructs we need to recognize:
            //
            //           module                                              ;
            // [export]  module <module-name> [<module-part>] [<attributes>] ;
            // [export]  import <module-name>                 [<attributes>] ;
            // [export]  import <module-part>                 [<attributes>] ;
            // [export]  import <header-name>                 [<attributes>] ;
            //
            // The leading module/export/import keyword should be the first
            // token of a logical line and only if certain characters appear
            // after module/import and all the tokens are on the same line,
            // then the line is recognized as a pseudo-directive; see p1857
            // for details.
            //
            // Additionally, when include is translated to an import, it's
            // normally replaced with the special __import keyword since it
            // may appear in C context.
            //
            if (bb == 0 && t.first)
            {
              const string& id (t.value); // Note: tracks t.

              // Handle the export prefix which can appear for both module
              // and import.
              //
              bool ex (false);
              if (id == "export")
              {
                if (l_->next (t) != type::identifier || t.first)
                {
                  n = false; // Could be module/import on next line.
                  continue;
                }

                ex = true;
                // Fall through.
              }

              if (id == "module")
              {
                location_value l (get_location (t));
                l_->next (t);

                if ((t.type == type::semi     ||
                     t.type == type::identifier) && !t.first)
                  parse_module (t, ex, move (l));
                else
                  n = false;
              }
              else if (id == "import" || id == "__import")
              {
                l_->next (t);

                if ((t.type == type::less     ||
                     t.type == type::colon    ||
                     t.type == type::string   ||
                     t.type == type::identifier) && !t.first)
                  parse_import (t, ex);
                else
                  n = false;
              }
            }
            continue;
          }
        default:
          continue;
        }

        break;
      }

      // We used to issue an error here but the diagnostics and, especially,
      // the location are not very helpful. While the compilers don't do a
      // much better job at it, there are often other semantic errors that are
      // more helpful and which we cannot match. So now we warn and let the
      // compiler fail.
      //
      // Another option would have been to postpone this diagnostics until
      // after the compiler fails (and thus also confirming that it indeed has
      // failed) but that would require propagating this state from apply() to
      // perform_update() and also making sure we issue this diagnostics even
      // if anything in between fails (probably by having it sitting in a
      // diag_frame). So let's keep it simple for now.
      //
      // @@ We now do that for missing include, so could do here as well.
      //
      if (bb != 0)
        warn (t) << (bb > 0 ? "missing '}'" : "extraneous '}'");

      if (module_marker_ && u.module_info.name.empty ())
        fail (*module_marker_) << "module declaration expected after "
                               << "global module fragment";

      checksum = l.checksum ();
      return u;
    }

    void parser::
    parse_module (token& t, bool ex, location_value l)
    {
      // enter: token after module keyword (l is the module keyword location)
      // leave: semi

      // Handle the leading 'module;' marker (p0713).
      //
      // Note that we don't bother diagnosing invalid/duplicate markers
      // leaving that to the compiler.
      //
      if (!ex && t.type == type::semi && !t.first)
      {
        module_marker_ = move (l);
        return;
      }

      // Otherwise it should be the start of the module name.
      //
      pair<string, bool> np (parse_module_name (t, true /* partition */));

      // Should be {}-balanced.
      //
      for (;
           t.type != type::eos && t.type != type::semi && !t.first;
           l_->next (t)) ;

      if (t.type != type::semi)
        fail (t) << "';' expected instead of " << t;
      else if (t.first)
        fail (t) << "';' must be on the same line";

      if (!u_->module_info.name.empty ())
        fail (l) << "multiple module declarations";

      u_->type =np.second
        ? (ex ? unit_type::module_intf_part : unit_type::module_impl_part)
        : (ex ? unit_type::module_intf      : unit_type::module_impl);
      u_->module_info.name = move (np.first);
    }

    void parser::
    parse_import (token& t, bool ex)
    {
      // enter: token after import keyword
      // leave: semi

      // Note that in import a partition can only be specified without a
      // module name. In other words, the following is invalid:
      //
      // module m;
      // import m:p;
      //
      string un;
      import_type ut;
      switch (t.type) // Start of module/header name.
      {
      case type::less:
      case type::string:
        {
          un = parse_header_name (t);
          ut = import_type::module_header;
          break;
        }
      case type::colon:
        {
          switch (u_->type)
          {
          case unit_type::module_intf:
          case unit_type::module_impl:
          case unit_type::module_intf_part:
          case unit_type::module_impl_part:
            break;
          default:
            fail (t) << "partition importation out of module purview";
          }

          // Add the module name to the partition so that code that doesn't
          // need to distinguish beetween different kinds of imports doesn't
          // have to.
          //
          un = u_->module_info.name + parse_module_part (t);
          ut = import_type::module_part;
          break;
        }
      case type::identifier:
        {
          un = parse_module_name (t, false /* partition */).first;
          ut = import_type::module_intf;
          break;
        }
      default:
        assert (false);
        return;
      }

      // Should be {}-balanced.
      //
      for (;
           t.type != type::eos && t.type != type::semi && !t.first;
           l_->next (t)) ;

      if (t.type != type::semi)
        fail (t) << "';' expected instead of " << t;
      else if (t.first)
        fail (t) << "';' must be on the same line";

      // For now we skip header units (see a comment on module type/info
      // string serialization in compile rule for details). Note that
      // currently parse_header_name() always returns empty name.
      //
      if (ut == import_type::module_header)
        return;

      // Ignore duplicates. We don't expect a large numbers of (direct)
      // imports so vector/linear search is probably more efficient than a
      // set.
      //
      auto& is (u_->module_info.imports);

      auto i (find_if (is.begin (), is.end (),
                       [&un] (const module_import& i)
                       {
                         return i.name == un;
                       }));

      if (i == is.end ())
        is.push_back (module_import {ut, move (un), ex, 0});
      else
        i->exported = i->exported || ex;
    }

    pair<string, bool /* partition */> parser::
    parse_module_name (token& t, bool part)
    {
      // enter: first token of module name
      // leave: token after module name

      string n;

      // <identifier>[ . <identifier>]* [<module-part>]
      //
      for (;; l_->next (t))
      {
        if (t.type != type::identifier)
          fail (t) << "module name expected instead of " << t;
        else if (t.first)
          fail (t) << "module name must be on the same line";

        n += t.value;

        if (l_->next (t) != type::dot || t.first)
          break;

        n += '.';
      }

      bool p (part && t.type == type::colon && !t.first);
      if (p)
        parse_module_part (t, n);

      return make_pair (move (n), p);
    }

    void parser::
    parse_module_part (token& t, string& n)
    {
      // enter: colon
      // leave: token after module partition

      n += ':';

      // : <identifier>[ . <identifier>]*
      //
      for (;;)
      {
        if (l_->next (t) != type::identifier)
          fail (t) << "partition name expected instead of " << t;
        else if (t.first)
          fail (t) << "partition name must be on the same line";

        n += t.value;

        if (l_->next (t) != type::dot || t.first)
          break;

        n += '.';
      }
    }

    string parser::
    parse_header_name (token& t)
    {
      // enter: first token of module name, either string or less
      // leave: token after module name

      string n;

      // NOTE: actual name is a TODO if/when we need it.
      //
      if (t.type == type::string)
        /*n = move (t.value)*/;
      else
      {
        while (l_->next (t) != type::greater)
        {
          if (t.type == type::eos || t.first)
            fail (t) << "closing '>' expected after header name" << endf;
        }
      }

      l_->next (t);
      return n;
    }
  }
}