summaryrefslogtreecommitdiffstats
path: root/src/qdoc/qdoc/src/qdoc/template_declaration.h
blob: a0aade682487e671437842183705a183e8622aa9 (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include <algorithm>
#include <cstdint>
#include <functional>
#include <numeric>
#include <optional>
#include <string>
#include <vector>

#include <QString>

/*
 * Represents a general declaration that has a form that can be
 * described by a type, name and initializer triplet, or any such form
 * that can be described by zero or more of those same parts.
 *
 * For example, it can be used to represent a C++ variable declaration
 * such as:
 *
 *     std::vector<int> foo = { 1, 2, 3 };
 *
 * Where `std::vector<int>` is the type, `foo` is the name and `{ 1, 2,
 * 3 }` is the initializer.
 *
 * Similarly, it can be used to represent a non-type template parameter
 * declaration, such as the `foo` parameter in:
 *
 *     template<int foo = 10>
 *
 * Where `int` is the type, `foo` is the name and `10` is the
 * initializer.
 *
 * An instance can be used to represent less information dense elements
 * by setting one or more of the fields as the empty string.
 *
 * For example, a template type parameter such as `T` in:
 *
 *     template<typename T = int>
 *
 * Can be represented by an instance that has an empty string as the
 * type, `T` as the name and `int` as the initializer.
 *
 * In general, it can be used to represent any such element that has
 * zero or more of the three components, albeit, in QDoc, it is
 * specifically intended to be used to represent various C++
 * declarations.
 *
 * All three fields are lowered stringified version of the original
 * declaration, so that the type should be used at the end of a
 * pipeline where the semantic property of the represented code are not
 * required.
 */
struct ValuedDeclaration
{
    struct PrintingPolicy
    {
        bool include_type = true;
        bool include_name = true;
        bool include_initializer = true;
    };

    std::string type;
    std::string name;
    std::string initializer;

    // KLUDGE: Workaround for
    // https://stackoverflow.com/questions/53408962/try-to-understand-compiler-error-message-default-member-initializer-required-be
    static PrintingPolicy default_printing_policy() { return PrintingPolicy{}; }

    /*
     * Constructs and returns a human-readable representation of this
     * declaration.
     *
     * The constructed string is formatted so that as to rebuild a
     * possible version of the C++ code that is modeled by an instance
     * of this type.
     *
     * Each component participates in the human-presentable version if
     * it they are not the empty string.
     *
     * The "type" and "name" component participate with their literal
     * representation.
     *
     * The "iniitlalizer" components contributes an equal symbol,
     * followed by a space followed by the literal representation of
     * the component.
     *
     * The component contributes in an ordered way, with "type"
     * contributing first, "name" contributing second and
     * "initializer" contributing last.
     *
     * Each contribution is separated by a space if the component that
     * comes before it, if any, has contributed to the human-readable
     * representation.
     *
     * For example, an instance of this type that has "type" component
     * "int", "name" component "foo" and "iniitializer" component
     * "100", would be represented as:
     *
     *  int foo = 100
     *
     *  Where "int" is the "type" component contribution, "foo" is the
     *  "name" component contribution and "= 100" is the "initializer"
     *  component contribution.
     *  Each of those contribution is separated by a space, as each
     *  "preceding" component has contributed to the representation.
     *
     *  If we provide a similar instance with, for example, the "type"
     *  and "name" components as the empty string, then the
     *  representation would be "= 100", which is the "initializer"
     *  component contribution, the only component that is not the
     *  empty string.
     *
     *  The policy argument allows to treat certain components as if
     *  they were the empty string.
     *
     *  For example, given an instance of this type that has "type"
     *  component "double", "name" component "bar" and "iniitializer"
     *  component "10.2", its human-readable representation would be
     *  "double bar = 10.2".
     *
     *  If the representation of that same instance was obtained by
     *  using a policy that excludes the "name" component, then that
     *  representation would be "double = 10.2", which is equivalent
     *  to the representation of an instance that is the same as the
     *  orginal one with the "name" component as the empty string.
     */
    inline std::string to_std_string(PrintingPolicy policy = default_printing_policy()) const
    {
        std::string s{};

        if (!type.empty() && policy.include_type)
            s += (s.empty() ? "" : " ") + type;

        if (!name.empty() && policy.include_name)
            s += (s.empty() ? "" : " ") + name;

        if (!initializer.empty() && policy.include_initializer)
            s += (s.empty() ? "= " : " = ") + initializer;

        return s;
    }
};

struct RelaxedTemplateParameter;

struct TemplateDeclarationStorage
{
    std::vector<RelaxedTemplateParameter> parameters;

    inline std::string to_std_string() const;
};

/*
 * Represents a C++ template parameter.
 *
 * The model used by this representation is a slighly simplified
 * model.
 *
 * In the model, template parameters are one of:
 *
 *    - A type template parameter.
 *    - A non type template parameter.
 *    - A template template parameter.
 *
 * Furthermore, each parameter can:
 *
 *     - Be a parameter pack.
 *     - Carry an additional template declaration (as a template template
 *       parameter would).
 *     - Have no declared type.
 *     - Have no declared name.
 *     - Have no declared initializer.
 *
 * Due to this simplified model certain incorrect parameters can be
 * represented.
 *
 * For example, it might be possible to represent a parameter pack
 * that has a default initializer, a non-type template parameter that
 * has no type or a template template parameter that carries no
 * template declaration.
 *
 * The model further elides some of the semantic that might be carried
 * by a parameter.
 * For example, the model has no specific concept for template
 * constraints.
 *
 * Template parameters can be represented as instances of the type.
 *
 * For example, a type template parameter `typename T` can be
 * represented as the following instance:
 *
 * RelaxedTemplateParameter{
 *     RelaxedTemplateParameter::Kind::TypeTemplateParameter,
 *     false,
 *     {
 *         "",
 *         "T",
 *         ""
 *     },
 *     {}
 * };
 *
 * And a non-type template parameter pack "int... Args" as:
 *
 * RelaxedTemplateParameter{
 *     RelaxedTemplateParameter::Kind::NonTypeTemplateParameter,
 *     true,
 *     {
 *         "int",
 *         "Args",
 *         ""
 *     },
 *     {}
 * };
 *
 * Due to the relaxed constraint and the representable incorrect
 * parameters, the type is intended to be used for data that is
 * already validated and known to be correct, such as data that is
 * extracted from Clang.
 */
struct RelaxedTemplateParameter
{
    enum class Kind : std::uint8_t {
        TypeTemplateParameter,
        NonTypeTemplateParameter,
        TemplateTemplateParameter
    };

    Kind kind;
    bool is_parameter_pack;
    ValuedDeclaration valued_declaration;
    std::optional<TemplateDeclarationStorage> template_declaration;

    /*
     * Constructs and returns a human-readable representation of this
     * parameter.
     *
     * The constructed string is formatted so that as to rebuild a
     * possible version of the C++ code that is modeled by an instance
     * of this type.
     *
     * The format of the representation varies based on the "kind" of
     * the parameter.
     *
     *   - A "TypeTemplateParameter", is constructed as the
     *     concatenation of the literal "typename", followed by the
     *     literal "..." if the parameter is a pack, followed by the
     *      human-readable representaion of "valued_declaration".
     *
     *      If the human-readable representation of
     *      "valued_declaration" is not the empty string, it is
     *      preceded by a space when it contributes to the
     *      representation.
     *
     *      For example, the C++ type template parameter "typename Foo
     *      = int", would be represented by the instance:
     *
     *        RelaxedTemplateParameter{
     *            RelaxedTemplateParameter::Kind::TypeTemplateParameter,
     *            false,
     *            {
     *                "",
     *                "Foo",
     *                "int"
     *            },
     *            {}
     *        };
     *
     *      And its representation would be:
     *
     *        typename Foo = int
     *
     *      Where "typename" is the added literal and "Foo = int" is
     *      the representation for "valued_declaration", with a space
     *      in-between the two contributions.
     *
     *    - A "NonTypeTemplateParameter", is constructed by the
     *      contribution of the "type" compoment of "valued_declaration",
     *      followed by the literal "..." if the parameter is a pack,
     *      followed by the human-presentable version of
     *      "valued_declaration" without its "type" component
     *      contribution.
     *
     *      If the contribution of the "type" component of
     *      "valued_declaration" is not empty, the next contribution is
     *      preceded by a space.
     *
     *      For example, the C++ non-type template parameter "int...
     *      SIZE", would be represented by the instance:
     *
     *
     *        RelaxedTemplateParameter{
     *            RelaxedTemplateParameter::Kind::NonTypeTemplateParameter,
     *            true,
     *            {
     *                "int",
     *                "SIZE",
     *                ""
     *            },
     *            {}
     *        };
     *
     *      And its representation would be:
     *
     *        int... SIZE
     *
     *      Where "int" is the "type" component contribution of
     *      "valued_declaration", "..." is the added literal due to
     *      the parameter being a pack and " SIZE" being the
     *      human-readable representation of "valued_declaration"
     *      without its "type" component contribution, preceded by a
     *      space.
     *
     *    - A "TemplateTemplateParameter", is constructed by the
     *      contribution of the human-presentable representation of
     *      "template_declaration", followed by the representation of
     *      this parameter if it was a "TypeTemplateParameter", with a
     *      space between the two contributions if the
     *      human-presentable representation of "template_declaration"
     *      is not empty.
     *
     *      For example, the C++ template template template parameter
     *      "template<typename> T", would be represented by the
     *      instance:
     *
     *
     *        RelaxedTemplateParameter{
     *            RelaxedTemplateParameter::Kind::TemplateTemplateParameter,
     *            false,
     *            {
     *                "",
     *                "T",
     *                ""
     *            },
     *            {
     *              RelaxedTemplateParameter{
     *                RelaxedTemplateParameter::Kind::TypeTemplateParameter,
     *                false,
     *                {
     *                    "",
     *                    "",
     *                    ""
     *                },
     *                {}
     *              }
     *            }
     *        };
     *
     *      And its representation would be:
     *
     *        template <typename> typename T
     *
     *      Where "template <typename>" human-presentable version of
     *      "template_declaration" and "typename T" is the
     *      human-presentable version of this parameter if it was a
     *      type template parameter.
     *
     *      With a space between the two contributions.
     */
    inline std::string to_std_string() const
    {
        switch (kind) {
        // TODO: This can probably be moved under the template
        // template parameter case and reused through a fallback.
        case Kind::TypeTemplateParameter: {
            std::string valued_declaration_string = valued_declaration.to_std_string();

            return std::string("typename") + (is_parameter_pack ? "..." : "")
                    + (valued_declaration_string.empty() ? "" : " ") + valued_declaration_string;
        }
        case Kind::NonTypeTemplateParameter: {
            std::string type_string = valued_declaration.type + (is_parameter_pack ? "..." : "");

            return type_string + (type_string.empty() ? "" : " ")
                    + valued_declaration.to_std_string(
                            ValuedDeclaration::PrintingPolicy{ false, true, true });
        }
        case Kind::TemplateTemplateParameter: {
            std::string valued_declaration_string = valued_declaration.to_std_string();

            return (template_declaration ? (*template_declaration).to_std_string() + " " : "")
                    + "typename" + (is_parameter_pack ? "..." : "")
                    + (valued_declaration_string.empty() ? "" : " ") + valued_declaration_string;
        }
        default:
            return "";
        }
    }
};

/*
 * Represents a C++ template declaration as a collection of template
 * parameters.
 *
 * The parameters for the declaration follow the same relaxed rules as
 * `RelaxedTemplateParameter` and inherit the possibility of
 * representing incorrect declarations.
 *
 * Due to the relaxed constraint and the representable incorrect
 * parameters, the type is intended to be used for data that is
 * already validated and known to be correct, such as data that is
 * extracted from Clang.
 */
struct RelaxedTemplateDeclaration : TemplateDeclarationStorage
{
    inline QString to_qstring() const { return QString::fromStdString(to_std_string()); }
};

/*
 * Constructs and returns a human-readable representation of this
 * declaration.
 *
 * The constructed string is formatted so as to rebuild a
 * possible version of the C++ code that is modeled by an instance
 * of this type.
 *
 * The representation of a declaration is constructed by the literal
 * "template <", followed by the human-presentable version of each
 * parameter in "parameters", with a comma and a space between each
 * parameter, followed by a closing literal ">".
 *
 * For example, the empty declaration is represented as "template <>".
 *
 * While a template declaration that has a type template parameter
 * "Foo" with initializer "int" and a non-type template parameter pack
 * with type "int" and name "S" would be represented as:
 *
 *   template <typename Foo = int, int... S>
 */
inline std::string TemplateDeclarationStorage::to_std_string() const
{
    if (parameters.empty())
        return "template <>";

    return "template <"
            + std::accumulate(std::next(parameters.cbegin()), parameters.cend(),
                              parameters.front().to_std_string(),
                              [](auto &&acc, const RelaxedTemplateParameter &parameter) {
                                  return acc + ", " + parameter.to_std_string();
                              })
            + ">";
}

/*
 * Returns true if the two template declaration represented by left
 * and right are substitutable.
 *
 * QDoc uses a simplified model for template declarations and,
 * similarly, uses a simplified model of "substitutability".
 *
 * Two declarations are substitutable if:
 *
 *  - They have the same amount of parameters
 *  - For each pair of parameters with the same postion:
 *    - They have the same kind
 *    - They are both parameter packs or both are not parameter packs
 *    - If they are non-type template parameters then they have the same type
 *    - If they are both template template parameters then they both
 *      carry an additional template declaration and the additional
 *      template declarations are substitutable
 *
 *  This means that in the simplified models, we generally ignore default arguments, name and such.
 *
 *  This model does not follow the way C++ performs disambiguation but
 *  should be enough to handle most cases in the documentation.
 */
inline bool are_template_declarations_substitutable(const TemplateDeclarationStorage& left, const TemplateDeclarationStorage& right) {
    static auto are_template_parameters_substitutable = [](const RelaxedTemplateParameter& left, const RelaxedTemplateParameter& right) {
        if (left.kind != right.kind) return false;
        if (left.is_parameter_pack != right.is_parameter_pack) return false;

        if (left.kind == RelaxedTemplateParameter::Kind::NonTypeTemplateParameter &&
            (left.valued_declaration.type != right.valued_declaration.type))
            return false;

        if (left.kind == RelaxedTemplateParameter::Kind::TemplateTemplateParameter) {
            if (!left.template_declaration && right.template_declaration) return false;
            if (left.template_declaration && !right.template_declaration) return false;

            if (left.template_declaration && right.template_declaration)
                return are_template_declarations_substitutable(*left.template_declaration, *right.template_declaration);
        }

        return true;
    };

    const auto& left_parameters = left.parameters;
    const auto& right_parameters = right.parameters;

    if (left_parameters.size() != right_parameters.size()) return false;

    return std::transform_reduce(left_parameters.cbegin(), left_parameters.cend(), right_parameters.cbegin(),
        true,
        std::logical_and<bool>{},
        are_template_parameters_substitutable
    );
}