Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4codegen_p.h
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#ifndef QV4CODEGEN_P_H
6#define QV4CODEGEN_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qqmljsastvisitor_p.h>
20#include <private/qqmljsengine_p.h>
21#include <private/qqmljsast_p.h>
22#include <private/qqmljsdiagnosticmessage_p.h>
23#include <private/qv4compiler_p.h>
24#include <private/qv4compilercontext_p.h>
25#include <private/qv4util_p.h>
26#include <private/qv4bytecodegenerator_p.h>
27#include <private/qv4calldata_p.h>
28
29#include <QtCore/qsharedpointer.h>
30#include <QtCore/qxpfunctional.h>
31
32#include <stack>
33
34QT_BEGIN_NAMESPACE
35
36namespace QV4 {
37
38namespace Moth {
39struct Instruction;
40}
41
42namespace CompiledData {
43struct CompilationUnit;
44}
45
46namespace Compiler {
47
48struct ControlFlow;
49struct ControlFlowCatch;
51
70
72{
73 static CodegenWarningInterface iface;
74 return &iface;
75}
76
77class Q_QML_COMPILER_EXPORT Codegen: protected QQmlJS::AST::Visitor
78{
79protected:
82public:
85 bool storeSourceLocations = false);
86
87 static bool isNameGlobal(QAnyStringView name);
89
93
96
97public:
101 bool allVolatile = false;
103 public:
105 if (allVolatile)
106 return true;
108 }
109
111 void setAllVolatile() { allVolatile = true; }
112 };
113 class RValue {
115 enum Type {
116 Invalid,
118 StackSlot,
119 Const
120 } type;
121 union {
124 };
125
126 public:
135 RValue r;
136 r.codegen = codegen;
138 return r;
139 }
141 RValue r;
142 r.codegen = codegen;
143 r.type = Const;
144 r.constant = value;
145 return r;
146 }
147
148 bool operator==(const RValue &other) const;
149
150 bool isValid() const { return type != Invalid; }
151 bool isAccumulator() const { return type == Accumulator; }
152 bool isStackSlot() const { return type == StackSlot; }
153 bool isConst() const { return type == Const; }
154
157 return theStackSlot;
158 }
159
161 Q_ASSERT(isConst());
162 return constant;
163 }
164
166 void loadInAccumulator() const;
167 };
168 struct Reference {
183
184 bool isLValue() const { return !isReadonly && type > Accumulator; }
185
187 {
188 type = t;
189 codegen = cg;
190 }
191
193 constant(0),
194 name(name),
195 isArgOrEval(false),
196 isReadonly(false),
197 isReferenceToConst(false),
198 requiresTDZCheck(false),
201 isVolatile(false),
202 global(false),
203 qmlGlobal(false),
206 isOptional(false),
208 {}
209
210 Reference(const Reference &) = default;
211 Reference(Reference &&) = default;
212 Reference &operator =(const Reference &) = default;
213 Reference &operator =(Reference &&) = default;
214
215 bool operator==(const Reference &other) const;
216 bool operator!=(const Reference &other) const
217 { return !(*this == other); }
218
219 bool isValid() const { return type != Invalid; }
221 switch (type) {
222 case Name:
223 case Member:
224 case Subscript:
225 case SuperProperty:
226 return true;
227 default:
228 return requiresTDZCheck;
229 }
230 }
231 bool isConstant() const { return type == Const; }
232 bool isAccumulator() const { return type == Accumulator; }
233 bool isSuper() const { return type == Super; }
234 bool isSuperProperty() const { return type == SuperProperty; }
235 bool isStackSlot() const { return type == StackSlot; }
236 bool isRegister() const {
237 return isStackSlot();
238 }
239
244 return Reference(cg, Super);
245 }
256 r.index = index;
257 r.scope = scope;
258 return r;
259 }
262 r.index = index;
263 return r;
264 }
266 Reference r(cg, Name);
267 r.name = name;
268 return r;
269 }
270 static Reference
305 r.isReadonly = true;
306 return r;
307 }
310 r.isReadonly = true;
311 // ### Optimize this. Functions that are not derived constructors or arrow functions can't have an
312 // empty this object
313 r.requiresTDZCheck = true;
314 return r;
315 }
316
317 RValue asRValue() const;
318 Reference asLValue() const;
319
322
325
327 void storeOnStack(int tempIndex) const;
330
332
333 bool storeWipesAccumulator() const;
334 void loadInAccumulator() const;
335
336 int nameAsIndex() const {
337 Q_ASSERT(type == Name);
338 return codegen->registerString(name);
339 }
340
342 if (Q_UNLIKELY(!isStackSlot()))
344 return theStackSlot;
345 }
346
354
355 void checkTDZBeforeAssignment() const;
356
357 union {
360 struct { // Scoped arguments/Local
361 int index;
362 int scope;
363 };
364 struct {
367 };
368 struct {
370 union {
373 };
374 };
375 Moth::StackSlot property; // super property
376 };
378 Codegen *codegen = nullptr;
379
397
398 private:
399 void storeAccumulator() const;
401 void tdzCheck(bool requiresCheck, bool throwsReferenceError) const;
404 };
405
416
419
421 int getter = -1; // index in _module->functions or -1 if not set
422 int setter = -1;
424
425 bool hasGetter() const { return getter >= 0; }
426 bool hasSetter() const { return setter >= 0; }
427 };
428protected:
429
430 enum Format { ex, cx, nx };
431 class Result {
433
434 const BytecodeGenerator::Label *_iftrue = nullptr;
435 const BytecodeGenerator::Label *_iffalse = nullptr;
436 Format _format = ex;
438 bool _trueBlockFollowsCondition = false;
439
440 public:
441 explicit Result(const QString &name)
442 : _result(name)
443 , _requested(ex)
444 {}
445
446 explicit Result(const Reference &lrvalue)
448 , _requested(ex)
449 {}
450
453
465
468 return _iftrue;
469 }
470
473 return _iffalse;
474 }
475
476 Format format() const {
477 return _format;
478 }
479
481 {
482 if (_requested == f) {
483 _format = f;
484 return true;
485 }
486 return false;
487 }
488
492
493 const Reference &result() const {
494 return _result;
495 }
496
498 _result = result;
499 }
500
503 }
504
507 }
508 };
509
511 int leaveContext();
512public:
514 int leaveBlock() { return leaveContext(); }
515protected:
516 void leaveLoop();
517
528
530
531 void addCJump();
532
533public:
557
558 // Returns index in _module->functions
562
563protected:
569
571 {
572 if (!ast || hasError())
573 return Reference();
574
575 pushExpr(name);
576 ast->accept(this);
577 return popResult();
578 }
579
580 inline void accept(QQmlJS::AST::Node *node)
581 {
582 if (!hasError() && node)
583 node->accept(this);
584 }
585
586 void program(QQmlJS::AST::Program *ast);
590
597
599
600 void emitReturn(const Reference &expr);
601
602 // nodes
607 bool visit(QQmlJS::AST::Catch *ast) override;
625
630
632
634
635 // expressions
677
678 // statements
679 bool visit(QQmlJS::AST::Block *ast) override;
697
698 // ui object members
705
707 const QQmlJS::SourceLocation &loc);
708 virtual void throwSyntaxError(const QQmlJS::SourceLocation &loc, const QString &detail);
709 virtual void throwReferenceError(const QQmlJS::SourceLocation &loc, const QString &detail);
711 {
713 QStringLiteral("Maximum statement or expression depth exceeded"));
714 }
715
716public:
722
723 ErrorType errorType() const { return _errorType; }
724 bool hasError() const { return _errorType != NoError; }
726 QUrl url() const;
727
731 struct Arguments { int argc; int argv; bool hasSpread; };
734
738
740
743
744
746 const QString &name, bool lhs,
748
750 bool generateUnitData = true);
752 bool debugMode, const QString &url, const QString &sourceCode,
754
756 bool debugMode, const QString &url, const QV4::Value &value);
757
758 Context *currentContext() const { return _context; }
760
761 void loadClosure(int index);
762
763 Module *module() const { return _module; }
764
770
771protected:
772 friend class ScanFunctions;
773 friend struct ControlFlow;
774 friend struct ControlFlowCatch;
775 friend struct ControlFlowFinally;
776
779 inline Reference exprResult() const { return m_expressions.back().result(); }
781
782 inline bool exprAccept(Format f) { return m_expressions.back().accept(f); }
783
784 inline const Result &currentExpr() const { return m_expressions.back(); }
785
787 inline void pushExpr(const Result &expr) { m_expressions.push_back(expr); }
789
791 {
794 return result;
795 }
796
800 return result;
801 }
802
814 bool useFastLookups = true;
816 bool insideSwitch = false;
821
829
832
834
839
841 {
842 public:
848
851
852 void unblock() const
854
855 void reblock() const
857
858 private:
859 Codegen *_cg;
860 bool _saved;
861 bool _onoff;
862 };
863
864private:
869 const QString &detail);
872 bool isDeleteExpression = false);
874 void generateThrowException(const QString &type, const QString &text = QString());
875};
876
877}
878
879}
880
881QT_END_NAMESPACE
882
883#endif // QV4CODEGEN_P_H
Codegen::VolatileMemoryLocations scan(AST::Node *s)
bool visit(ArrayMemberExpression *) override
CodegenWarningInterface * defaultCodegenWarningInterface()
Definition qjsvalue.h:24
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
static Node * completionStatement(StatementList *list)
static CompletionState completionState(StatementList *list)
static QSOperator::Op baseOp(int op)
CompletionState
static bool endsWithReturn(Module *module, Node *node)
static void setJumpOutLocation(QV4::Moth::BytecodeGenerator *bytecodeGenerator, const Statement *body, const SourceLocation &fallback)
static constexpr const QLatin1StringView s_globalNames[]