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
80
82{
83 static CodegenWarningInterface iface;
84 return &iface;
85}
86
87class Q_QML_COMPILER_EXPORT Codegen: protected QQmlJS::AST::Visitor
88{
89protected:
92public:
95 bool storeSourceLocations = false);
96
97 static bool isNameGlobal(QAnyStringView name);
100
104
107
108public:
112 bool allVolatile = false;
114 public:
116 if (allVolatile)
117 return true;
119 }
120
122 void setAllVolatile() { allVolatile = true; }
123 };
124 class RValue {
126 enum Type {
127 Invalid,
129 StackSlot,
130 Const
131 } type;
132 union {
135 };
136
137 public:
146 RValue r;
147 r.codegen = codegen;
149 return r;
150 }
152 RValue r;
153 r.codegen = codegen;
154 r.type = Const;
155 r.constant = value;
156 return r;
157 }
158
159 bool operator==(const RValue &other) const;
160
161 bool isValid() const { return type != Invalid; }
162 bool isAccumulator() const { return type == Accumulator; }
163 bool isStackSlot() const { return type == StackSlot; }
164 bool isConst() const { return type == Const; }
165
168 return theStackSlot;
169 }
170
172 Q_ASSERT(isConst());
173 return constant;
174 }
175
177 void loadInAccumulator() const;
178 };
179 struct Reference {
194
195 bool isLValue() const { return !isReadonly && type > Accumulator; }
196
198 {
199 type = t;
200 codegen = cg;
201 }
202
204 constant(0),
205 name(name),
206 isArgOrEval(false),
207 isReadonly(false),
208 isReferenceToConst(false),
209 requiresTDZCheck(false),
212 isVolatile(false),
213 global(false),
214 qmlGlobal(false),
217 isOptional(false),
219 {}
220
221 Reference(const Reference &) = default;
222 Reference(Reference &&) = default;
223 Reference &operator =(const Reference &) = default;
224 Reference &operator =(Reference &&) = default;
225
226 bool operator==(const Reference &other) const;
227 bool operator!=(const Reference &other) const
228 { return !(*this == other); }
229
230 bool isValid() const { return type != Invalid; }
232 switch (type) {
233 case Name:
234 case Member:
235 case Subscript:
236 case SuperProperty:
237 return true;
238 default:
239 return requiresTDZCheck;
240 }
241 }
242 bool isConstant() const { return type == Const; }
243 bool isAccumulator() const { return type == Accumulator; }
244 bool isSuper() const { return type == Super; }
245 bool isSuperProperty() const { return type == SuperProperty; }
246 bool isStackSlot() const { return type == StackSlot; }
247 bool isRegister() const {
248 return isStackSlot();
249 }
250
255 return Reference(cg, Super);
256 }
267 r.index = index;
268 r.scope = scope;
269 return r;
270 }
273 r.index = index;
274 return r;
275 }
277 Reference r(cg, Name);
278 r.name = name;
279 return r;
280 }
281 static Reference
316 r.isReadonly = true;
317 return r;
318 }
321 r.isReadonly = true;
322 // ### Optimize this. Functions that are not derived constructors or arrow functions can't have an
323 // empty this object
324 r.requiresTDZCheck = true;
325 return r;
326 }
327
328 RValue asRValue() const;
329 Reference asLValue() const;
330
333
336
338 void storeOnStack(int tempIndex) const;
341
343
344 bool storeWipesAccumulator() const;
345 void loadInAccumulator() const;
346
347 int nameAsIndex() const {
348 Q_ASSERT(type == Name);
349 return codegen->registerString(name);
350 }
351
353 if (Q_UNLIKELY(!isStackSlot()))
355 return theStackSlot;
356 }
357
365
366 void checkTDZBeforeAssignment() const;
367
368 union {
371 struct { // Scoped arguments/Local
372 int index;
373 int scope;
374 };
375 struct {
378 };
379 struct {
381 union {
384 };
385 };
386 Moth::StackSlot property; // super property
387 };
389 Codegen *codegen = nullptr;
390
408
409 private:
410 void storeAccumulator() const;
412 void tdzCheck(bool requiresCheck, bool throwsReferenceError) const;
415 };
416
427
430
432 int getter = -1; // index in _module->functions or -1 if not set
433 int setter = -1;
435
436 bool hasGetter() const { return getter >= 0; }
437 bool hasSetter() const { return setter >= 0; }
438 };
439protected:
440
441 enum Format { ex, cx, nx };
442 class Result {
444
445 const BytecodeGenerator::Label *_iftrue = nullptr;
446 const BytecodeGenerator::Label *_iffalse = nullptr;
447 Format _format = ex;
449 bool _trueBlockFollowsCondition = false;
450
451 public:
452 explicit Result(const QString &name)
453 : _result(name)
454 , _requested(ex)
455 {}
456
457 explicit Result(const Reference &lrvalue)
459 , _requested(ex)
460 {}
461
464
476
479 return _iftrue;
480 }
481
484 return _iffalse;
485 }
486
487 Format format() const {
488 return _format;
489 }
490
492 {
493 if (_requested == f) {
494 _format = f;
495 return true;
496 }
497 return false;
498 }
499
503
504 const Reference &result() const {
505 return _result;
506 }
507
509 _result = result;
510 }
511
514 }
515
518 }
519 };
520
522 int leaveContext();
523public:
525 int leaveBlock() { return leaveContext(); }
526protected:
527 void leaveLoop();
528
539
541
542 void addCJump();
543
544public:
568
569 // Returns index in _module->functions
573
574protected:
580
582 {
583 if (!ast || hasError())
584 return Reference();
585
586 pushExpr(name);
587 ast->accept(this);
588 return popResult();
589 }
590
591 inline void accept(QQmlJS::AST::Node *node)
592 {
593 if (!hasError() && node)
594 node->accept(this);
595 }
596
597 void program(QQmlJS::AST::Program *ast);
601
608
610
611 void emitReturn(const Reference &expr);
612
613 // nodes
618 bool visit(QQmlJS::AST::Catch *ast) override;
636
641
643
645
646 // expressions
688
689 // statements
690 bool visit(QQmlJS::AST::Block *ast) override;
708
709 // ui object members
716
718 const QQmlJS::SourceLocation &loc);
719 virtual void throwSyntaxError(const QQmlJS::SourceLocation &loc, const QString &detail);
720 virtual void throwReferenceError(const QQmlJS::SourceLocation &loc, const QString &detail);
722 {
724 QStringLiteral("Maximum statement or expression depth exceeded"));
725 }
726
727public:
733
734 ErrorType errorType() const { return _errorType; }
735 bool hasError() const { return _errorType != NoError; }
737 QUrl url() const;
738
742 struct Arguments { int argc; int argv; bool hasSpread; };
745
749
751
754
755
757 const QString &name, bool lhs,
759
761 bool generateUnitData = true);
765
768 const QV4::Value &value);
769
770 Context *currentContext() const { return _context; }
772
773 void loadClosure(int index);
774
775 Module *module() const { return _module; }
776
782
783protected:
784 friend class ScanFunctions;
785 friend struct ControlFlow;
786 friend struct ControlFlowCatch;
787 friend struct ControlFlowFinally;
788
791 inline Reference exprResult() const { return m_expressions.back().result(); }
793
794 inline bool exprAccept(Format f) { return m_expressions.back().accept(f); }
795
796 inline const Result &currentExpr() const { return m_expressions.back(); }
797
799 inline void pushExpr(const Result &expr) { m_expressions.push_back(expr); }
801
803 {
806 return result;
807 }
808
812 return result;
813 }
814
826 bool useFastLookups = true;
828 bool insideSwitch = false;
832
840
843
845
850
852 {
853 public:
859
862
863 void unblock() const
865
866 void reblock() const
868
869 private:
870 Codegen *_cg;
871 bool _saved;
872 bool _onoff;
873 };
874
875private:
880 const QString &detail);
883 bool isDeleteExpression = false);
885 void generateThrowException(const QString &type, const QString &text = QString());
886};
887
888}
889
890}
891
892QT_END_NAMESPACE
893
894#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 void setJumpOutLocation(QV4::Moth::BytecodeGenerator *bytecodeGenerator, const Statement *body, const SourceLocation &fallback)
static constexpr const QLatin1StringView s_globalNames[]