summaryrefslogtreecommitdiffstats
path: root/src/scxml/qscxmlexecutablecontent_p.h
blob: 59803302fa8a542c34f7c36352aa32c338e75881 (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
503
504
505
506
507
508
509
510
511
512
513
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtScxml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QSCXMLEXECUTABLECONTENT_P_H
#define QSCXMLEXECUTABLECONTENT_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtScxml/qscxmlexecutablecontent.h>
#include <QtScxml/private/qscxmltabledata_p.h>
#include <QtScxml/private/qscxmlcompiler_p.h>
#include <QtCore/qtextstream.h>

#ifndef BUILD_QSCXMLC
#include <QtScxml/qscxmldatamodel.h>
#include <QtScxml/qscxmlstatemachine.h>
#endif // BUILD_QSCXMLC

QT_BEGIN_NAMESPACE

namespace QScxmlExecutableContent {

static inline bool operator<(const EvaluatorInfo &ei1, const EvaluatorInfo &ei2)
{
    if (ei1.expr != ei2.expr)
        return ei1.expr < ei2.expr;
    else
        return ei1.context < ei2.context;
}

static inline bool operator<(const AssignmentInfo &ai1, const AssignmentInfo &ai2)
{
    if (ai1.dest != ai2.dest)
        return ai1.dest < ai2.dest;
    else if (ai1.expr != ai2.expr)
        return ai1.expr < ai2.expr;
    else
        return ai1.context < ai2.context;
}

static inline bool operator<(const ForeachInfo &fi1, const ForeachInfo &fi2)
{
    if (fi1.array != fi2.array) return fi1.array < fi2.array;
    if (fi1.item != fi2.item) return fi1.item < fi2.item;
    if (fi1.index != fi2.index) return fi1.index < fi2.index;
    return fi1.context < fi2.context;
}

#if defined(Q_CC_MSVC) || defined(Q_CC_GNU)
#pragma pack(push, 4) // 4 == sizeof(qint32)
#endif

template <typename T>
struct Array
{
    qint32 count;
    // T[] data;
    T *data() { return const_cast<T *>(const_data()); }
    const T *const_data() const { return reinterpret_cast<const T *>(reinterpret_cast<const char *>(this) + sizeof(Array<T>)); }

    const T &at(int pos) const { return *(const_data() + pos); }
    int dataSize() const { return count * sizeof(T) / sizeof(qint32); }
    int size() const { return sizeof(Array<T>) / sizeof(qint32) + dataSize(); }
};

struct Q_SCXML_EXPORT Instruction
{
    enum InstructionType: qint32 {
        Sequence = 1,
        Sequences,
        Send,
        Raise,
        Log,
        JavaScript,
        Assign,
        Initialize,
        If,
        Foreach,
        Cancel,
        DoneData
    } instructionType;
};

struct Q_SCXML_EXPORT DoneData: Instruction
{
    StringId location;
    StringId contents;
    EvaluatorId expr;
    Array<ParameterInfo> params;

    static InstructionType kind() { return Instruction::DoneData; }
};

struct Q_SCXML_EXPORT InstructionSequence: Instruction
{
    qint32 entryCount; // the amount of qint32's that the instructions take up
    // Instruction[] instructions;

    static InstructionType kind() { return Instruction::Sequence; }
    const InstructionId *instructions() const
    {
        return reinterpret_cast<const InstructionId *>(this)
                + sizeof(InstructionSequence) / sizeof(qint32);
    }
    int size() const { return sizeof(InstructionSequence) / sizeof(qint32) + entryCount; }
};

struct Q_SCXML_EXPORT InstructionSequences: Instruction
{
    qint32 sequenceCount;
    qint32 entryCount; // the amount of qint32's that the sequences take up
    // InstructionSequence[] sequences;

    static InstructionType kind() { return Instruction::Sequences; }
    const InstructionSequence *sequences() const {
        return reinterpret_cast<const InstructionSequence *>(
                    reinterpret_cast<const InstructionId *>(this)
                    + sizeof(InstructionSequences) / sizeof(qint32));
    }
    int size() const { return sizeof(InstructionSequences)/sizeof(qint32) + entryCount; }
    const InstructionId *at(int pos) const
    {
        const InstructionId *seq = reinterpret_cast<const InstructionId *>(sequences());
        while (pos--) {
            seq += reinterpret_cast<const InstructionSequence *>(seq)->size();
        }
        return seq;
    }
};

struct Q_SCXML_EXPORT Send: Instruction
{
    StringId instructionLocation;
    StringId event;
    EvaluatorId eventexpr;
    StringId type;
    EvaluatorId typeexpr;
    StringId target;
    EvaluatorId targetexpr;
    StringId id;
    StringId idLocation;
    StringId delay;
    EvaluatorId delayexpr;
    StringId content;
    EvaluatorId contentexpr;
    Array<StringId> namelist;
//    Array<Param> params;

    static InstructionType kind() { return Instruction::Send; }

    int paramsOffset() const
    {
        return sizeof(Send) / sizeof(qint32) + namelist.dataSize();
    }

    int size() const
    {
        return paramsOffset() + params()->size();
    }

    const Array<ParameterInfo> *params() const {
        return reinterpret_cast<const Array<ParameterInfo> *>(
                    reinterpret_cast<const InstructionId *>(this) + paramsOffset());
    }

    Array<ParameterInfo> *params() {
        return reinterpret_cast<Array<ParameterInfo> *>(
                    reinterpret_cast<InstructionId *>(this) + paramsOffset());
    }

    static int calculateExtraSize(int paramCount, int nameCount) {
        return 1 + paramCount * sizeof(ParameterInfo) / sizeof(qint32)
                + nameCount * sizeof(StringId) / sizeof(qint32);
    }
};

struct Q_SCXML_EXPORT Raise: Instruction
{
    StringId event;

    static InstructionType kind() { return Instruction::Raise; }
    int size() const { return sizeof(Raise) / sizeof(qint32); }
};

struct Q_SCXML_EXPORT Log: Instruction
{
    StringId label;
    EvaluatorId expr;

    static InstructionType kind() { return Instruction::Log; }
    int size() const { return sizeof(Log) / sizeof(qint32); }
};

struct Q_SCXML_EXPORT JavaScript: Instruction
{
    EvaluatorId go;

    static InstructionType kind() { return Instruction::JavaScript; }
    int size() const { return sizeof(JavaScript) / sizeof(qint32); }
};

struct Q_SCXML_EXPORT Assign: Instruction
{
    EvaluatorId expression;

    static InstructionType kind() { return Instruction::Assign; }
    int size() const { return sizeof(Assign) / sizeof(qint32); }
};

struct Q_SCXML_EXPORT Initialize: Instruction
{
    EvaluatorId expression;

    static InstructionType kind() { return Instruction::Initialize; }
    int size() const { return sizeof(Initialize) / sizeof(qint32); }
};

struct Q_SCXML_EXPORT If: Instruction
{
    Array<EvaluatorId> conditions;
    // InstructionSequences blocks;
    const InstructionSequences *blocks() const {
        return reinterpret_cast<const InstructionSequences *>(
                    reinterpret_cast<const InstructionId *>(this) + sizeof(If) / sizeof(qint32)
                    + conditions.dataSize());
    }

    static InstructionType kind() { return Instruction::If; }
    int size() const
    {
        return sizeof(If) / sizeof(qint32) + blocks()->size() + conditions.dataSize();
    }
};

struct Q_SCXML_EXPORT Foreach: Instruction
{
    EvaluatorId doIt;
    InstructionSequence block;

    static InstructionType kind() { return Instruction::Foreach; }
    int size() const { return sizeof(Foreach) / sizeof(qint32) + block.entryCount; }
    const InstructionId *blockstart() const
    {
        return reinterpret_cast<const InstructionId *>(&block);
    }
};

struct Q_SCXML_EXPORT Cancel: Instruction
{
    StringId sendid;
    EvaluatorId sendidexpr;

    static InstructionType kind() { return Instruction::Cancel; }
    int size() const { return sizeof(Cancel) / sizeof(qint32); }
};

struct StateTable {
    int version;
    int name;
    enum: int {
        InvalidDataModel = -1,
        NullDataModel = 0,
        EcmaScriptDataModel = 1,
        CppDataModel = 2
    } dataModel;
    int childStates; // offset into offsets
    int initialTransition;
    int initialSetup;
    enum: int { InvalidBinding = -1, EarlyBinding = 0, LateBinding = 1 } binding;
    int maxServiceId;
    int stateOffset, stateCount;
    int transitionOffset, transitionCount;
    int arrayOffset, arraySize;

    enum { terminator = 0xc0ff33 };
    enum { InvalidIndex = -1 };

    struct State {
        int name;
        int parent;
        enum: int {
            Invalid = -1,
            Normal = 0,
            Parallel = 1,
            Final = 2,
            ShallowHistory = 3,
            DeepHistory = 4
        } type;
        int initialTransition;
        int initInstructions;
        int entryInstructions;
        int exitInstructions;
        int doneData;
        int childStates; // offset into arrays
        int transitions; // offset into arrays
        int serviceFactoryIds; // offset into arrays

        State()
            : name(InvalidIndex)
            , parent(InvalidIndex)
            , type(Invalid)
            , initialTransition(InvalidIndex)
            , initInstructions(InvalidIndex)
            , entryInstructions(InvalidIndex)
            , exitInstructions(InvalidIndex)
            , doneData(InvalidIndex)
            , childStates(InvalidIndex)
            , transitions(InvalidIndex)
            , serviceFactoryIds(InvalidIndex)
        {}

        bool isAtomic() const
        { return childStates == InvalidIndex; }

        bool isCompound() const
        { return type == Normal && childStates != InvalidIndex; }

        bool parentIsScxmlElement() const
        { return parent == InvalidIndex; }

        bool isHistoryState() const
        { return type == ShallowHistory || type == DeepHistory; }

        bool isParallel() const
        { return type == Parallel; }
    };

    struct Transition {
        int events; // offset into offsets
        int condition;
        enum: int {
            Invalid = -1,
            Internal = 0,
            External = 1,
            Synthetic = 2
        } type;
        int source;
        int targets; // offset into offsets
        int transitionInstructions;

        Transition()
            : events(InvalidIndex)
            , condition(InvalidIndex)
            , type(Invalid)
            , source(InvalidIndex)
            , targets(InvalidIndex)
            , transitionInstructions(InvalidIndex)
        {}
    };

    struct Array {
        Array(const int *start): start(start) {}
        int size() const { return *start; }
        bool isValid() const { return start != nullptr; }

        int operator[](int idx) const {
            Q_ASSERT(idx >= 0);
            Q_ASSERT(idx < size());
            return *(start + idx + 1);
        }

        struct const_iterator: public std::iterator<std::forward_iterator_tag, int, ptrdiff_t,
                                                    const int *, const int &>
        {
            const_iterator(const Array &a, int pos): a(a), pos(pos) {}

            const_iterator &operator++() {
                if (pos < a.size()) ++pos;
                return *this;
            }

            bool operator==(const const_iterator &other) const
            { return &other.a == &a && other.pos == pos; }

            bool operator!=(const StateTable::Array::const_iterator &other)
            { return !this->operator==(other); }

            int operator*() const {
                if (pos < a.size())
                    return a[pos];
                else
                    return -1;
            }

        private:
            const Array &a;
            int pos;
        };

        const_iterator begin() const
        { return const_iterator(*this, 0); }

        const_iterator end() const
        { return const_iterator(*this, size()); }

    private:
        const int *start;
    };

    StateTable()
        : version(InvalidIndex)
        , name(InvalidIndex)
        , dataModel(InvalidDataModel)
        , childStates(InvalidIndex)
        , initialTransition(InvalidIndex)
        , initialSetup(InvalidIndex)
        , binding(InvalidBinding)
        , maxServiceId(InvalidIndex)
        , stateOffset(InvalidIndex), stateCount(InvalidIndex)
        , transitionOffset(InvalidIndex), transitionCount(InvalidIndex)
        , arrayOffset(InvalidIndex), arraySize(InvalidIndex)
    {}

    const State &state(int idx) const
    {
        Q_ASSERT(idx >= 0);
        Q_ASSERT(idx < stateCount);
        return reinterpret_cast<const State *>(
                    reinterpret_cast<const int *>(this) + stateOffset)[idx];
    }

    const Transition &transition(int idx) const
    {
        Q_ASSERT(idx >= 0);
        Q_ASSERT(idx < transitionCount);
        return reinterpret_cast<const Transition *>(
                    reinterpret_cast<const int *>(this) + transitionOffset)[idx];
    }

    const Array array(int idx) const
    {
        Q_ASSERT(idx < arraySize);
        if (idx >= 0) {
            const int *start = reinterpret_cast<const int *>(this) + arrayOffset + idx;
            Q_ASSERT(*start + idx < arraySize);
            return Array(start);
        } else {
            return Array(nullptr);
        }
    }
};

#if defined(Q_CC_MSVC) || defined(Q_CC_GNU)
#pragma pack(pop)
#endif

} // QScxmlExecutableContent namespace

class QScxmlExecutionEngine
{
    Q_DISABLE_COPY(QScxmlExecutionEngine)

public:
    QScxmlExecutionEngine(QScxmlStateMachine *stateMachine);

    bool execute(QScxmlExecutableContent::ContainerId ip, const QVariant &extraData = QVariant());

private:
    const QScxmlExecutableContent::InstructionId *step(
            const QScxmlExecutableContent::InstructionId *ip, bool *ok);

    QScxmlStateMachine *stateMachine;
    QVariant extraData;
};

QT_END_NAMESPACE

#endif // QSCXMLEXECUTABLECONTENT_P_H