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

#include <libbuild2/functions-name.hxx>

#include <libbuild2/scope.hxx>
#include <libbuild2/function.hxx>
#include <libbuild2/variable.hxx>
#include <libbuild2/algorithm.hxx>

using namespace std;

namespace build2
{
  extern bool
  functions_sort_flags (optional<names>); // functions-builtin.cxx

  // Convert name to target'ish name (see below for the 'ish part). Return
  // raw/unprocessed data in case this is an unknown target type (or called
  // out of scope). See scope::find_target_type() for details. Allow out-
  // qualified names (out is discarded).
  //
  static pair<const target_type*, optional<string>>
  to_target_type (const scope* s, name& n, const name& o = name ())
  {
    if (n.pair && !o.directory ())
      fail << "name pair in names";

    return s != nullptr
      ? s->find_target_type (n, location ())
      : pair<const target_type*, optional<string>> {nullptr, nullopt};
  }

  static pair<name, optional<string>>
  to_target_name (const scope* s, name&& n, const name& o = name ())
  {
    auto rp (to_target_type (s, n, o));

    if (rp.first != nullptr)
      n.type = rp.first->name;

    return make_pair (move (n), move (rp.second));
  }

  const target&
  to_target (const scope& s, name&& n, name&& o)
  {
    if (const target* r = search_existing (n, s, o.dir))
      return *r;

    // Inside recipes we don't treat `{}` as special so a literal target name
    // will have no type and won't be found, which is confusing as hell.
    //
    bool typed (n.typed ());

    diag_record dr (fail);

    dr << "target "
       << (n.pair ? names {move (n), move (o)} : names {move (n)})
       << " not found";

    if (!typed)
      dr << info << "wrap it in ([names] ...) if this is literal target name "
         << "specified inside recipe";

    dr << endf;
  }

  const target&
  to_target (const scope& s, names&& ns)
  {
    assert (ns.size () == (ns[0].pair ? 2 : 1));

    name o;
    return to_target (s, move (ns[0]), move (ns[0].pair ? ns[1] : o));
  }

  static bool
  is_a (const scope* s, name&& n, const name& o, names&& t)
  {
    if (s == nullptr)
      fail << "name.is_a() called out of scope";

    string tts (convert<string> (move (t)));
    const target_type* tt (s->find_target_type (tts));
    if (tt == nullptr)
      fail << "unknown target type " << tts;

    const target_type* ntt (to_target_type (s, n, o).first);
    if (ntt == nullptr)
      fail << "unknown target type " << n.type << " in " << n;

    return ntt->is_a (*tt);
  }

  static names
  filter (const scope* s, names ns, names ts, bool out)
  {
    if (s == nullptr)
      fail << "name." << (out ? "filter_out" : "filter")
           << "() called out of scope";

    small_vector<const target_type*, 1> tts;
    for (const name& n: ts)
    {
      if (!n.simple ())
        fail << "invalid target type name " << n;

      if (n.pair)
        fail << "pair in target type name " << n;

      const target_type* tt (s->find_target_type (n.value));
      if (tt == nullptr)
        fail << "unknown target type " << n.value;

      tts.push_back (tt);
    }

    names r;
    for (auto i (ns.begin ()); i != ns.end (); ++i)
    {
      name& n (*i);
      bool p (n.pair);

      const target_type* ntt (to_target_type (s, n, p ? *++i : name ()).first);
      if (ntt == nullptr)
        fail << "unknown target type " << n.type << " in " << n;

      if ((find_if (tts.begin (), tts.end (),
                    [ntt] (const target_type* tt)
                    {
                      return ntt->is_a (*tt);
                    }) != tts.end ()) != out)
      {
        r.push_back (move (n));
        if (p)
          r.push_back (move (*i));
      }
    }

    return r;
  }

  void
  name_functions (function_map& m)
  {
    // These functions treat a name as a target/prerequisite name.
    //
    // While on one hand it feels like calling them target.name(), etc., would
    // have been more appropriate, on the other hand they can also be called
    // on prerequisite names. They also won't always return the same result as
    // if we were interrogating an actual target (e.g., the directory may be
    // relative). Plus we now have functions that can only be called on
    // targets (see functions-target.cxx).
    //
    function_family f (m, "name");

    // Note: let's leave this undocumented for now since it's not often needed
    // and is a can of worms.
    //
    // Note that we must handle NULL values (relied upon by the parser
    // to provide conversion semantics consistent with untyped values).
    //
    f["string"] += [](name* n)
    {
      return n != nullptr ? to_string (move (*n)) : string ();
    };

    // $name(<names>)
    //
    // Return the name of a target (or a list of names for a list of targets).
    //
    f["name"] += [](const scope* s, name n)
    {
      return to_target_name (s, move (n)).first.value;
    };
    f["name"] += [](const scope* s, names ns)
    {
      small_vector<string, 1> r;

      for (auto i (ns.begin ()); i != ns.end (); ++i)
      {
        name& n (*i);
        r.push_back (
          to_target_name (s, move (n), n.pair ? *++i : name ()).first.value);
      }

      if (r.size () == 1)
        return value (move (r[0]));

      return value (strings (make_move_iterator (r.begin ()),
                             make_move_iterator (r.end ())));
    };

    // $extension(<name>)
    //
    // Return the extension of a target.
    //
    // Note that this function returns `null` if the extension is unspecified
    // (default) and empty string if it's specified as no extension.
    //
    f["extension"] += [](const scope* s, name n)
    {
      return to_target_name (s, move (n)).second;
    };
    f["extension"] += [](const scope* s, names ns)
    {
      // Note: can't do multiple due to NULL semantics.
      //
      auto i (ns.begin ());

      name& n (*i);
      const name& o (n.pair ? *++i : name ());

      if (++i != ns.end ())
        fail << "invalid name value: multiple names"; // Like in convert().

      return to_target_name (s, move (n), o).second;
    };

    // $directory(<names>)
    //
    // Return the directory of a target (or a list of directories for a list
    // of targets).
    //
    f["directory"] += [](const scope* s, name n)
    {
      return to_target_name (s, move (n)).first.dir;
    };
    f["directory"] += [](const scope* s, names ns)
    {
      small_vector<dir_path, 1> r;

      for (auto i (ns.begin ()); i != ns.end (); ++i)
      {
        name& n (*i);
        r.push_back (
          to_target_name (s, move (n), n.pair ? *++i : name ()).first.dir);
      }

      if (r.size () == 1)
        return value (move (r[0]));

      return value (dir_paths (make_move_iterator (r.begin ()),
                               make_move_iterator (r.end ())));
    };

    // $target_type(<names>)
    //
    // Return the target type name of a target (or a list of target type names
    // for a list of targets).
    //
    f["target_type"] += [](const scope* s, name n)
    {
      return to_target_name (s, move (n)).first.type;
    };
    f["target_type"] += [](const scope* s, names ns)
    {
      small_vector<string, 1> r;

      for (auto i (ns.begin ()); i != ns.end (); ++i)
      {
        name& n (*i);
        r.push_back (
          to_target_name (s, move (n), n.pair ? *++i : name ()).first.type);
      }

      if (r.size () == 1)
        return value (move (r[0]));

      return value (strings (make_move_iterator (r.begin ()),
                             make_move_iterator (r.end ())));
    };

    // $project(<name>)
    //
    // Return the project of a target or `null` if not project-qualified.
    //
    f["project"] += [](const scope* s, name n)
    {
      return to_target_name (s, move (n)).first.proj;
    };
    f["project"] += [](const scope* s, names ns)
    {
      // Note: can't do multiple due to NULL semantics.
      //
      auto i (ns.begin ());

      name& n (*i);
      const name& o (n.pair ? *++i : name ());

      if (++i != ns.end ())
        fail << "invalid name value: multiple names"; // Like in convert().

      return to_target_name (s, move (n), o).first.proj;
    };

    // $is_a(<name>, <target-type>)
    //
    // Return true if the <name>'s target type is-a <target-type>. Note that
    // this is a dynamic type check that takes into account target type
    // inheritance.
    //
    f["is_a"] += [](const scope* s, name n, names t)
    {
      return is_a (s, move (n), name (), move (t));
    };
    f["is_a"] += [](const scope* s, names ns, names t)
    {
      auto i (ns.begin ());

      name& n (*i);
      const name& o (n.pair ? *++i : name ());

      if (++i != ns.end ())
        fail << "invalid name value: multiple names"; // Like in convert().

      return is_a (s, move (n), o, move (t));
    };

    // $filter(<names>, <target-types>)
    // $filter_out(<names>, <target-types>)
    //
    // Return names with target types which are-a (`filter`) or not are-a
    // (`filter_out`) one of <target-types>. See `$is_a()` for background.
    //
    f["filter"] += [](const scope* s, names ns, names ts)
    {
      return filter (s, move (ns), move (ts), false /* out */);
    };

    f["filter_out"] += [](const scope* s, names ns, names ts)
    {
      return filter (s, move (ns), move (ts), true /* out */);
    };

    // $size(<names>)
    //
    // Return the number of elements in the sequence.
    //
    f["size"] += [] (names ns)
    {
      size_t n (0);

      for (auto i (ns.begin ()); i != ns.end (); ++i)
      {
        ++n;
        if (i->pair && !(++i)->directory ())
          fail << "name pair in names";
      }

      return n;
    };

    // $sort(<names>[, <flags>])
    //
    // Sort names in ascending order.
    //
    // The following flags are supported:
    //
    //     dedup - in addition to sorting also remove duplicates
    //
    f["sort"] += [] (names ns, optional<names> fs)
    {
      //@@ TODO: shouldn't we do this in a pair-aware manner?

      sort (ns.begin (), ns.end ());

      if (functions_sort_flags (move (fs)))
        ns.erase (unique (ns.begin(), ns.end()), ns.end ());

      return ns;
    };

    // $find(<names>, <name>)
    //
    // Return true if the name sequence contains the specified name.
    //
    f["find"] += [](names vs, names v)
    {
      //@@ TODO: shouldn't we do this in a pair-aware manner?

      return find (vs.begin (), vs.end (),
                   convert<name> (move (v))) != vs.end ();
    };

    // $find_index(<names>, <name>)
    //
    // Return the index of the first element in the name sequence that is
    // equal to the specified name or `$size(names)` if none is found.
    //
    f["find_index"] += [](names vs, names v)
    {
      //@@ TODO: shouldn't we do this in a pair-aware manner?

      auto i (find (vs.begin (), vs.end (), convert<name> (move (v))));
      return i != vs.end () ? i - vs.begin () : vs.size ();
    };

    // Name-specific overloads from builtins.
    //
    function_family fb (m, "builtin");

    // Note that while we should normally handle NULL values (relied upon by
    // the parser to provide concatenation semantics consistent with untyped
    // values), the result will unlikely be what the user expected. So for now
    // we keep it a bit tighter.
    //
    fb[".concat"] += [](dir_path d, name n)
    {
      d /= n.dir;
      n.dir = move (d);
      return n;
    };
  }
}