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
72
74{
75 static CodegenWarningInterface iface;
76 return &iface;
77}
78
79class Q_QML_COMPILER_EXPORT Codegen: protected QQmlJS::AST::Visitor
80{
81protected:
84public:
87 bool storeSourceLocations = false);
88
89 static bool isNameGlobal(QAnyStringView name);
91
95
98
99public:
103 bool allVolatile = false;
105 public:
107 if (allVolatile)
108 return true;
110 }
111
113 void setAllVolatile() { allVolatile = true; }
114 };
115 class RValue {
117 enum Type {
118 Invalid,
120 StackSlot,
121 Const
122 } type;
123 union {
126 };
127
128 public:
137 RValue r;
138 r.codegen = codegen;
140 return r;
141 }
143 RValue r;
144 r.codegen = codegen;
145 r.type = Const;
146 r.constant = value;
147 return r;
148 }
149
150 bool operator==(const RValue &other) const;
151
152 bool isValid() const { return type != Invalid; }
153 bool isAccumulator() const { return type == Accumulator; }
154 bool isStackSlot() const { return type == StackSlot; }
155 bool isConst() const { return type == Const; }
156
159 return theStackSlot;
160 }
161
163 Q_ASSERT(isConst());
164 return constant;
165 }
166
168 void loadInAccumulator() const;
169 };
170 struct Reference {
185
186 bool isLValue() const { return !isReadonly && type > Accumulator; }
187
189 {
190 type = t;
191 codegen = cg;
192 }
193
195 constant(0),
196 name(name),
197 isArgOrEval(false),
198 isReadonly(false),
199 isReferenceToConst(false),
200 requiresTDZCheck(false),
203 isVolatile(false),
204 global(false),
205 qmlGlobal(false),
208 isOptional(false),
210 {}
211
212 Reference(const Reference &) = default;
213 Reference(Reference &&) = default;
214 Reference &operator =(const Reference &) = default;
215 Reference &operator =(Reference &&) = default;
216
217 bool operator==(const Reference &other) const;
218 bool operator!=(const Reference &other) const
219 { return !(*this == other); }
220
221 bool isValid() const { return type != Invalid; }
223 switch (type) {
224 case Name:
225 case Member:
226 case Subscript:
227 case SuperProperty:
228 return true;
229 default:
230 return requiresTDZCheck;
231 }
232 }
233 bool isConstant() const { return type == Const; }
234 bool isAccumulator() const { return type == Accumulator; }
235 bool isSuper() const { return type == Super; }
236 bool isSuperProperty() const { return type == SuperProperty; }
237 bool isStackSlot() const { return type == StackSlot; }
238 bool isRegister() const {
239 return isStackSlot();
240 }
241
246 return Reference(cg, Super);
247 }
258 r.index = index;
259 r.scope = scope;
260 return r;
261 }
264 r.index = index;
265 return r;
266 }
268 Reference r(cg, Name);
269 r.name = name;
270 return r;
271 }
272 static Reference
307 r.isReadonly = true;
308 return r;
309 }
312 r.isReadonly = true;
313 // ### Optimize this. Functions that are not derived constructors or arrow functions can't have an
314 // empty this object
315 r.requiresTDZCheck = true;
316 return r;
317 }
318
319 RValue asRValue() const;
320 Reference asLValue() const;
321
324
327
329 void storeOnStack(int tempIndex) const;
332
334
335 bool storeWipesAccumulator() const;
336 void loadInAccumulator() const;
337
338 int nameAsIndex() const {
339 Q_ASSERT(type == Name);
340 return codegen->registerString(name);
341 }
342
344 if (Q_UNLIKELY(!isStackSlot()))
346 return theStackSlot;
347 }
348
356
357 void checkTDZBeforeAssignment() const;
358
359 union {
362 struct { // Scoped arguments/Local
363 int index;
364 int scope;
365 };
366 struct {
369 };
370 struct {
372 union {
375 };
376 };
377 Moth::StackSlot property; // super property
378 };
380 Codegen *codegen = nullptr;
381
399
400 private:
401 void storeAccumulator() const;
403 void tdzCheck(bool requiresCheck, bool throwsReferenceError) const;
406 };
407
418
421
423 int getter = -1; // index in _module->functions or -1 if not set
424 int setter = -1;
426
427 bool hasGetter() const { return getter >= 0; }
428 bool hasSetter() const { return setter >= 0; }
429 };
430protected:
431
432 enum Format { ex, cx, nx };
433 class Result {
435
436 const BytecodeGenerator::Label *_iftrue = nullptr;
437 const BytecodeGenerator::Label *_iffalse = nullptr;
438 Format _format = ex;
440 bool _trueBlockFollowsCondition = false;
441
442 public:
443 explicit Result(const QString &name)
444 : _result(name)
445 , _requested(ex)
446 {}
447
448 explicit Result(const Reference &lrvalue)
450 , _requested(ex)
451 {}
452
455
467
470 return _iftrue;
471 }
472
475 return _iffalse;
476 }
477
478 Format format() const {
479 return _format;
480 }
481
483 {
484 if (_requested == f) {
485 _format = f;
486 return true;
487 }
488 return false;
489 }
490
494
495 const Reference &result() const {
496 return _result;
497 }
498
500 _result = result;
501 }
502
505 }
506
509 }
510 };
511
513 int leaveContext();
514public:
516 int leaveBlock() { return leaveContext(); }
517protected:
518 void leaveLoop();
519
530
532
533 void addCJump();
534
535public:
559
560 // Returns index in _module->functions
564
565protected:
571
573 {
574 if (!ast || hasError())
575 return Reference();
576
577 pushExpr(name);
578 ast->accept(this);
579 return popResult();
580 }
581
582 inline void accept(QQmlJS::AST::Node *node)
583 {
584 if (!hasError() && node)
585 node->accept(this);
586 }
587
588 void program(QQmlJS::AST::Program *ast);
592
599
601
602 void emitReturn(const Reference &expr);
603
604 // nodes
609 bool visit(QQmlJS::AST::Catch *ast) override;
627
632
634
636
637 // expressions
679
680 // statements
681 bool visit(QQmlJS::AST::Block *ast) override;
699
700 // ui object members
707
709 const QQmlJS::SourceLocation &loc);
710 virtual void throwSyntaxError(const QQmlJS::SourceLocation &loc, const QString &detail);
711 virtual void throwReferenceError(const QQmlJS::SourceLocation &loc, const QString &detail);
713 {
715 QStringLiteral("Maximum statement or expression depth exceeded"));
716 }
717
718public:
724
725 ErrorType errorType() const { return _errorType; }
726 bool hasError() const { return _errorType != NoError; }
728 QUrl url() const;
729
733 struct Arguments { int argc; int argv; bool hasSpread; };
736
740
742
745
746
748 const QString &name, bool lhs,
750
752 bool generateUnitData = true);
754 bool debugMode, const QString &url, const QString &sourceCode,
756
758 bool debugMode, const QString &url, const QV4::Value &value);
759
760 Context *currentContext() const { return _context; }
762
763 void loadClosure(int index);
764
765 Module *module() const { return _module; }
766
772
773protected:
774 friend class ScanFunctions;
775 friend struct ControlFlow;
776 friend struct ControlFlowCatch;
777 friend struct ControlFlowFinally;
778
781 inline Reference exprResult() const { return m_expressions.back().result(); }
783
784 inline bool exprAccept(Format f) { return m_expressions.back().accept(f); }
785
786 inline const Result &currentExpr() const { return m_expressions.back(); }
787
789 inline void pushExpr(const Result &expr) { m_expressions.push_back(expr); }
791
793 {
796 return result;
797 }
798
802 return result;
803 }
804
816 bool useFastLookups = true;
818 bool insideSwitch = false;
823
831
834
836
841
843 {
844 public:
850
853
854 void unblock() const
856
857 void reblock() const
859
860 private:
861 Codegen *_cg;
862 bool _saved;
863 bool _onoff;
864 };
865
866private:
871 const QString &detail);
874 bool isDeleteExpression = false);
876 void generateThrowException(const QString &type, const QString &text = QString());
877};
878
879}
880
881}
882
883QT_END_NAMESPACE
884
885#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[]