| 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/. */ |
| 6 |
|
| 7 |
#ifndef frontend_LabelEmitter_h |
| 8 |
#define frontend_LabelEmitter_h |
| 9 |
|
| 10 |
#include "mozilla/Attributes.h" // MOZ_STACK_CLASS |
| 11 |
#include "mozilla/Maybe.h" // Maybe |
| 12 |
|
| 13 |
#include "frontend/BytecodeControlStructures.h" // LabelControl |
| 14 |
|
| 15 |
namespace js { |
| 16 |
namespace frontend { |
| 17 |
|
| 18 |
struct BytecodeEmitter; |
| 19 |
class TaggedParserAtomIndex; |
| 20 |
|
| 21 |
// Class for emitting labeled statement. |
| 22 |
// |
| 23 |
// Usage: (check for the return value is omitted for simplicity) |
| 24 |
// |
| 25 |
// `label: expr;` |
| 26 |
// LabelEmitter le(this); |
| 27 |
// le.emitLabel(name_of_label); |
| 28 |
// emit(expr); |
| 29 |
// le.emitEnd(); |
| 30 |
// |
| 31 |
class MOZ_STACK_CLASS LabelEmitter { |
| 32 |
BytecodeEmitter* bce_; |
| 33 |
|
| 34 |
mozilla::Maybe<LabelControl> controlInfo_; |
| 35 |
|
| 36 |
#ifdef DEBUG |
| 37 |
// The state of this emitter. |
| 38 |
// |
| 39 |
// +-------+ emitLabel +-------+ emitEnd +-----+ |
| 40 |
// | Start |---------->| Label |-------->| End | |
| 41 |
// +-------+ +-------+ +-----+ |
| 42 |
enum class State { |
| 43 |
// The initial state. |
| 44 |
Start, |
| 45 |
|
| 46 |
// After calling emitLabel. |
| 47 |
Label, |
| 48 |
|
| 49 |
// After calling emitEnd. |
| 50 |
End |
| 51 |
}; |
| 52 |
State state_ = State::Start; |
| 53 |
#endif |
| 54 |
|
| 55 |
public: |
| 56 |
explicit LabelEmitter(BytecodeEmitter* bce) : bce_(bce) {} |
| 57 |
|
| 58 |
void emitLabel(TaggedParserAtomIndex name); |
| 59 |
[[nodiscard]] bool emitEnd(); |
| 60 |
}; |
| 61 |
|
| 62 |
} /* namespace frontend */ |
| 63 |
} /* namespace js */ |
| 64 |
|
| 65 |
#endif /* frontend_LabelEmitter_h */ |