aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmldom/qqmldomscriptelements.cpp
blob: 4bde2abd09058819f696802234290c4c1172feb2 (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qqmldom_utils_p.h"
#include "qqmldomitem_p.h"
#include "qqmldompath_p.h"
#include "qqmldomscriptelements_p.h"
#include <memory>
#include <utility>
#include <variant>

using namespace QQmlJS::Dom::ScriptElements;
using QQmlJS::Dom::DomType;
using QQmlJS::Dom::ScriptElement;
using QQmlJS::Dom::ScriptElementVariant;

/*!
   \internal
   \class ScriptElementBase

   The base class for all script elements.

   Derived classes should implement createFileLocations, DomElement::updatePathFromOwner and
   DomBase::iterateDirectSubpaths. Furthermore, they need their own DomType enum.

   updatePathFromOwner and createFileLocations should be called on the script element root node
   after it was constructed for the DomItem-wrapping to work correctly. Without it, methods like
   iterateDirectSubpaths and all the stuff in DomItem will not work.

   createFileLocations does not work without having the pathFromOwner set
   first via updatePathFromOwner.

   In derived classes, the updatePathFromOwner-implementation should call the base implementation
   and also call recursively updatePathFromOwner on the derived class's children.

   See \l ScriptElementBase::createFileLocations for the createFileLocations implementation in
   derived classes.

   Derived classes need to implement iterateDirectSubpaths to comply with the DomItem interface.
*/

/*!
   \internal
   \fn ScriptElementBase::createFileLocations

   Usually, all the visits/recursive calls to DOM elements can be done using the DomItem interface,
   once all the DOM has been constructed.

   During construction, createFileLocations can be used to annotate the DOM representation with the
   corresponding source locations, which are needed, e.g., to find the corresponding DOM element
   from a certain text position. When called, createFileLocations sets an entry for itself in the
   FileLocationsTree.

   Derived classes should call the base implemenatation and recursively call createFileLocations on
   all their children.

   Usually, only the root of the script DOM element requires one createFileLocations call after
   construction \b{and} after a pathFromOwner was set using updatePathFromOwner.

*/

/*!
   \internal
   \class ScriptList

   A Helper class for writing script elements that contain lists, helps for implementing the
   recursive calls of iterateDirectSubpaths, updatePathFromOwner and createFileLocations.
*/

/*!
   \internal
    Helper for fields with elements in iterateDirectSubpaths.
 */
static bool wrap(const QQmlJS::Dom::DomItem &self, QQmlJS::Dom::DirectVisitor visitor, QStringView field,
                 const ScriptElementVariant &value)
{
    if (!value)
        return true;

    const bool b =
            self.dvItemField(visitor, field, [&self, field, &value]() -> QQmlJS::Dom::DomItem {
                const QQmlJS::Dom::Path pathFromOwner{ self.pathFromOwner().field(field) };
                return self.subScriptElementWrapperItem(value);
            });
    return b;
}

/*!
   \internal
    Helper for fields with lists in iterateDirectSubpaths.
 */
static bool wrap(const QQmlJS::Dom::DomItem &self, QQmlJS::Dom::DirectVisitor visitor, QStringView field,
                 const ScriptList &value)
{
    const bool b =
            self.dvItemField(visitor, field, [&self, field, &value]() -> QQmlJS::Dom::DomItem {
                const QQmlJS::Dom::Path pathFromOwner{ self.pathFromOwner().field(field) };
                return self.subListItem(value.asList(pathFromOwner));
            });
    return b;
}

bool GenericScriptElement::iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
{
    bool cont = true;
    for (auto it = m_children.begin(); it != m_children.end(); ++it) {
        cont &= std::visit(
                [&self, &visitor, &it](auto &&e) { return wrap(self, visitor, it->first, e); },
                it->second);
    }
    return cont;
}

void GenericScriptElement::updatePathFromOwner(const Path &p)
{
    BaseT::updatePathFromOwner(p);
    for (auto it = m_children.begin(); it != m_children.end(); ++it) {
        std::visit(qOverloadedVisitor{ [&p, &it](ScriptElementVariant &e) {
                                          e.base()->updatePathFromOwner(p.field(it->first));
                                      },
                                       [&p, &it](ScriptList &list) {
                                           list.updatePathFromOwner(p.field(it->first));
                                       } },
                   it->second);
    }
}

void GenericScriptElement::createFileLocations(const FileLocations::Tree &base)
{
    BaseT::createFileLocations(base);
    for (auto it = m_children.begin(); it != m_children.end(); ++it) {
        std::visit(
                qOverloadedVisitor{
                        [&base](ScriptElementVariant &e) { e.base()->createFileLocations(base); },
                        [&base](ScriptList &list) { list.createFileLocations(base); } },
                it->second);
    }
}

bool BlockStatement::iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
{
    // TODO: test me
    bool cont = true;
    cont &= wrap(self, visitor, Fields::statements, m_statements);
    return cont;
}

void BlockStatement::updatePathFromOwner(const Path &p)
{
    BaseT::updatePathFromOwner(p);
    m_statements.updatePathFromOwner(p.field(Fields::statements));
}

void BlockStatement::createFileLocations(const FileLocations::Tree &base)
{
    BaseT::createFileLocations(base);
    m_statements.createFileLocations(base);
}

bool IdentifierExpression::iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
{
    bool cont = true;
    cont &= self.dvValueField(visitor, Fields::identifier, m_name);
    return cont;
}

bool Literal::iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
{
    bool cont = true;
    std::visit([&cont, &visitor,
                &self](auto &&e) { cont &= self.dvValueField(visitor, Fields::value, e); },
               m_value);
    return cont;
}

bool IfStatement::iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
{
    // TODO: test me
    bool cont = true;
    cont &= wrap(self, visitor, Fields::condition, m_condition);
    cont &= wrap(self, visitor, Fields::consequence, m_consequence);
    cont &= wrap(self, visitor, Fields::alternative, m_alternative);
    return cont;
}

void IfStatement::updatePathFromOwner(const Path &p)
{
    BaseT::updatePathFromOwner(p);
    if (auto ptr = m_condition.base())
        ptr->updatePathFromOwner(p.field(Fields::condition));
    if (auto ptr = m_consequence.base())
        ptr->updatePathFromOwner(p.field(Fields::consequence));
    if (auto ptr = m_alternative.base())
        ptr->updatePathFromOwner(p.field(Fields::alternative));
}

void IfStatement::createFileLocations(const FileLocations::Tree &base)
{
    BaseT::createFileLocations(base);
    if (auto ptr = m_condition.base())
        ptr->createFileLocations(base);
    if (auto ptr = m_consequence.base())
        ptr->createFileLocations(base);
    if (auto ptr = m_alternative.base())
        ptr->createFileLocations(base);
}

bool ForStatement::iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
{
    bool cont = true;
    cont &= wrap(self, visitor, Fields::initializer, m_initializer);
    cont &= wrap(self, visitor, Fields::declarations, m_declarations);
    cont &= wrap(self, visitor, Fields::condition, m_condition);
    cont &= wrap(self, visitor, Fields::expression, m_expression);
    cont &= wrap(self, visitor, Fields::body, m_body);
    return cont;
}

void ForStatement::updatePathFromOwner(const Path &p)
{
    BaseT::updatePathFromOwner(p);
    if (auto ptr = m_initializer.base())
        ptr->updatePathFromOwner(p.field(Fields::initializer));
    if (auto ptr = m_declarations.base())
        ptr->updatePathFromOwner(p.field(Fields::declarations));
    if (auto ptr = m_condition.base())
        ptr->updatePathFromOwner(p.field(Fields::condition));
    if (auto ptr = m_expression.base())
        ptr->updatePathFromOwner(p.field(Fields::expression));
    if (auto ptr = m_body.base())
        ptr->updatePathFromOwner(p.field(Fields::body));
}

void ForStatement::createFileLocations(const FileLocations::Tree &base)
{
    BaseT::createFileLocations(base);
    if (auto ptr = m_initializer.base())
        ptr->createFileLocations(base);
    if (auto ptr = m_declarations.base())
        ptr->createFileLocations(base);
    if (auto ptr = m_condition.base())
        ptr->createFileLocations(base);
    if (auto ptr = m_expression.base())
        ptr->createFileLocations(base);
    if (auto ptr = m_body.base())
        ptr->createFileLocations(base);
}

bool BinaryExpression::iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
{
    bool cont = true;
    cont &= wrap(self, visitor, Fields::left, m_left);
    cont &= self.dvValueField(visitor, Fields::operation, m_operator);
    cont &= wrap(self, visitor, Fields::right, m_right);
    return cont;
}

void BinaryExpression::updatePathFromOwner(const Path &p)
{
    BaseT::updatePathFromOwner(p);
    if (auto ptr = m_left.base())
        ptr->updatePathFromOwner(p.field(Fields::left));
    if (auto ptr = m_right.base())
        ptr->updatePathFromOwner(p.field(Fields::right));
}

void BinaryExpression::createFileLocations(const FileLocations::Tree &base)
{
    BaseT::createFileLocations(base);
    if (auto ptr = m_left.base())
        ptr->createFileLocations(base);
    if (auto ptr = m_right.base())
        ptr->createFileLocations(base);
}

bool VariableDeclarationEntry::iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
{
    bool cont = true;
    cont &= self.dvValueField(visitor, Fields::scopeType, m_scopeType);
    cont &= wrap(self, visitor, Fields::identifier, m_identifier);
    cont &= wrap(self, visitor, Fields::initializer, m_initializer);
    return cont;
}

void VariableDeclarationEntry::updatePathFromOwner(const Path &p)
{
    BaseT::updatePathFromOwner(p);
    if (auto ptr = m_identifier.base())
        ptr->updatePathFromOwner(p.field(Fields::identifier));
    if (auto ptr = m_initializer.base())
        ptr->updatePathFromOwner(p.field(Fields::initializer));
}

void VariableDeclarationEntry::createFileLocations(const FileLocations::Tree &base)
{
    BaseT::createFileLocations(base);
    if (auto ptr = m_identifier.base())
        ptr->createFileLocations(base);
    if (auto ptr = m_initializer.base())
        ptr->createFileLocations(base);
}

bool VariableDeclaration::iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
{
    bool cont = true;
    cont &= wrap(self, visitor, Fields::declarations, m_declarations);
    return cont;
}

void VariableDeclaration::updatePathFromOwner(const Path &p)
{
    BaseT::updatePathFromOwner(p);
    m_declarations.updatePathFromOwner(p.field(Fields::declarations));
}

void VariableDeclaration::createFileLocations(const FileLocations::Tree &base)
{
    BaseT::createFileLocations(base);
    m_declarations.createFileLocations(base);
}

bool ReturnStatement::iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
{
    bool cont = true;
    cont &= wrap(self, visitor, Fields::expression, m_expression);
    return cont;
}

void ReturnStatement::updatePathFromOwner(const Path &p)
{
    BaseT::updatePathFromOwner(p);
    if (auto ptr = m_expression.base())
        ptr->updatePathFromOwner(p.field(Fields::expression));
}

void ReturnStatement::createFileLocations(const FileLocations::Tree &base)
{
    BaseT::createFileLocations(base);
    if (auto ptr = m_expression.base())
        ptr->createFileLocations(base);
}

void ScriptList::replaceKindForGenericChildren(DomType oldType, DomType newType)
{
    for (auto &it : m_list) {
        if (auto current = it.data()) {
            if (auto genericElement =
                        std::get_if<std::shared_ptr<ScriptElements::GenericScriptElement>>(
                                &*current)) {
                if ((*genericElement)->kind() == oldType)
                    (*genericElement)->setKind(newType);
            }
        }
    }
}