1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef frontend_ForInEmitter_h
8 #define frontend_ForInEmitter_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/Maybe.h"
15 #include "frontend/BytecodeControlStructures.h"
16 #include "frontend/TDZCheckCache.h"
21 struct BytecodeEmitter
;
24 // Class for emitting bytecode for for-in loop.
26 // Usage: (check for the return value is omitted for simplicity)
28 // `for (init in iterated) body`
29 // // headLexicalEmitterScope: lexical scope for init
30 // ForInEmitter forIn(this, headLexicalEmitterScope);
31 // forIn.emitIterated();
33 // forIn.emitInitialize();
37 // forIn.emitEnd(offset_of_for);
39 class MOZ_STACK_CLASS ForInEmitter
{
40 BytecodeEmitter
* bce_
;
43 // The stack depth before emitting initialize code inside loop.
44 int32_t loopDepth_
= 0;
47 mozilla::Maybe
<LoopControl
> loopInfo_
;
49 // The lexical scope to be freshened for each iteration. See the comment
50 // in `emitBody` for more details. Can be nullptr if there's no lexical
52 const EmitterScope
* headLexicalEmitterScope_
;
54 // Cache for the iterated value.
55 // (The cache for the iteration body is inside `loopInfo_`)
57 // The iterated value needs its own TDZCheckCache, separated from both the
58 // enclosing block and the iteration body, in order to make the sanity check
59 // in Ion work properly.
60 // In term of the execution order, the TDZCheckCache for the iterated value
61 // dominates the one for the iteration body, that means the checks in the
62 // iteration body is dead, and we can optimize them away. But the sanity
63 // check in Ion doesn't know it's dead.
64 // (see bug 1368360 for more context)
65 mozilla::Maybe
<TDZCheckCache
> tdzCacheForIteratedValue_
;
68 // The state of this emitter.
70 // +-------+ emitIterated +----------+ emitInitialize +------------+
71 // | Start |------------->| Iterated |--------------->| Initialize |-+
72 // +-------+ +----------+ +------------+ |
74 // +----------------------------------+
76 // | emitBody +------+ emitEnd +-----+
77 // +----------| Body |--------->| End |
83 // After calling emitIterated.
86 // After calling emitInitialize.
89 // After calling emitBody.
92 // After calling emitEnd.
95 State state_
= State::Start
;
99 ForInEmitter(BytecodeEmitter
* bce
,
100 const EmitterScope
* headLexicalEmitterScope
);
102 // Parameters are the offset in the source code for each character below:
104 // for ( var x in obj ) { ... }
108 [[nodiscard
]] bool emitIterated();
109 [[nodiscard
]] bool emitInitialize();
110 [[nodiscard
]] bool emitBody();
111 [[nodiscard
]] bool emitEnd(uint32_t forPos
);
114 } /* namespace frontend */
117 #endif /* frontend_ForInEmitter_h */