DefaultEmitter.h (1678B)
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_DefaultEmitter_h 8 #define frontend_DefaultEmitter_h 9 10 #include "mozilla/Attributes.h" // MOZ_STACK_CLASS 11 #include "mozilla/Maybe.h" // Maybe 12 13 #include "frontend/IfEmitter.h" // IfEmitter 14 15 namespace js { 16 namespace frontend { 17 18 struct BytecodeEmitter; 19 20 // Class for emitting default parameter or default value. 21 // 22 // Usage: (check for the return value is omitted for simplicity) 23 // 24 // `x = 10` in `function (x = 10) {}` 25 // // the value of arguments[0] is on the stack 26 // DefaultEmitter de(this); 27 // de.prepareForDefault(); 28 // emit(10); 29 // de.emitEnd(); 30 // 31 class MOZ_STACK_CLASS DefaultEmitter { 32 BytecodeEmitter* bce_; 33 34 mozilla::Maybe<IfEmitter> ifUndefined_; 35 36 #ifdef DEBUG 37 // The state of this emitter. 38 // 39 // +-------+ prepareForDefault +---------+ emitEnd +-----+ 40 // | Start |------------------>| Default |-------->| End | 41 // +-------+ +---------+ +-----+ 42 enum class State { 43 // The initial state. 44 Start, 45 46 // After calling prepareForDefault. 47 Default, 48 49 // After calling emitEnd. 50 End 51 }; 52 State state_ = State::Start; 53 #endif 54 55 public: 56 explicit DefaultEmitter(BytecodeEmitter* bce); 57 58 [[nodiscard]] bool prepareForDefault(); 59 [[nodiscard]] bool emitEnd(); 60 }; 61 62 } /* namespace frontend */ 63 } /* namespace js */ 64 65 #endif /* frontend_LabelEmitter_h */