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 union {
358 struct { // Scoped arguments/Local
359 int index;
360 int scope;
361 };
362 struct {
365 };
366 struct {
368 union {
371 };
372 };
373 Moth::StackSlot property; // super property
374 };
376 Codegen *codegen = nullptr;
377
395
396 private:
397 void storeAccumulator() const;
399 void tdzCheck(bool requiresCheck, bool throwsReferenceError) const;
402 };
403
414
417
419 int getter = -1; // index in _module->functions or -1 if not set
420 int setter = -1;
422
423 bool hasGetter() const { return getter >= 0; }
424 bool hasSetter() const { return setter >= 0; }
425 };
426protected:
427
428 enum Format { ex, cx, nx };
429 class Result {
431
432 const BytecodeGenerator::Label *_iftrue = nullptr;
433 const BytecodeGenerator::Label *_iffalse = nullptr;
434 Format _format = ex;
436 bool _trueBlockFollowsCondition = false;
437
438 public:
439 explicit Result(const QString &name)
440 : _result(name)
441 , _requested(ex)
442 {}
443
444 explicit Result(const Reference &lrvalue)
446 , _requested(ex)
447 {}
448
451
463
466 return _iftrue;
467 }
468
471 return _iffalse;
472 }
473
474 Format format() const {
475 return _format;
476 }
477
479 {
480 if (_requested == f) {
481 _format = f;
482 return true;
483 }
484 return false;
485 }
486
490
491 const Reference &result() const {
492 return _result;
493 }
494
496 _result = result;
497 }
498
501 }
502
505 }
506 };
507
509 int leaveContext();
510public:
512 int leaveBlock() { return leaveContext(); }
513protected:
514 void leaveLoop();
515
526
528
529 void addCJump();
530
531public:
555
556 // Returns index in _module->functions
560
561protected:
567
569 {
570 if (!ast || hasError())
571 return Reference();
572
573 pushExpr(name);
574 ast->accept(this);
575 return popResult();
576 }
577
578 inline void accept(QQmlJS::AST::Node *node)
579 {
580 if (!hasError() && node)
581 node->accept(this);
582 }
583
584 void program(QQmlJS::AST::Program *ast);
588
594
596
597 void emitReturn(const Reference &expr);
598
599 // nodes
604 bool visit(QQmlJS::AST::Catch *ast) override;
622
627
629
631
632 // expressions
674
675 // statements
676 bool visit(QQmlJS::AST::Block *ast) override;
694
695 // ui object members
702
704 const QQmlJS::SourceLocation &loc);
705 virtual void throwSyntaxError(const QQmlJS::SourceLocation &loc, const QString &detail);
706 virtual void throwReferenceError(const QQmlJS::SourceLocation &loc, const QString &detail);
708 {
710 QStringLiteral("Maximum statement or expression depth exceeded"));
711 }
712
713public:
719
720 ErrorType errorType() const { return _errorType; }
721 bool hasError() const { return _errorType != NoError; }
723 QUrl url() const;
724
728 struct Arguments { int argc; int argv; bool hasSpread; };
731
735
737
740
741
743 const QString &name, bool lhs,
745
747 bool generateUnitData = true);
749 bool debugMode, const QString &url, const QString &sourceCode,
751
753 bool debugMode, const QString &url, const QV4::Value &value);
754
755 Context *currentContext() const { return _context; }
757
758 void loadClosure(int index);
759
760 Module *module() const { return _module; }
761
767
768protected:
769 friend class ScanFunctions;
770 friend struct ControlFlow;
771 friend struct ControlFlowCatch;
772 friend struct ControlFlowFinally;
773
776 inline Reference exprResult() const { return m_expressions.back().result(); }
778
779 inline bool exprAccept(Format f) { return m_expressions.back().accept(f); }
780
781 inline const Result &currentExpr() const { return m_expressions.back(); }
782
784 inline void pushExpr(const Result &expr) { m_expressions.push_back(expr); }
786
788 {
791 return result;
792 }
793
797 return result;
798 }
799
811 bool useFastLookups = true;
813 bool insideSwitch = false;
818
826
829
831
836
838 {
839 public:
845
848
849 void unblock() const
851
852 void reblock() const
854
855 private:
856 Codegen *_cg;
857 bool _saved;
858 bool _onoff;
859 };
860
861private:
866 const QString &detail);
869 bool isDeleteExpression = false);
871 void generateThrowException(const QString &type, const QString &text = QString());
872};
873
874}
875
876}
877
878QT_END_NAMESPACE
879
880#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[]