| 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_EmitterScope_h |
| 8 |
#define frontend_EmitterScope_h |
| 9 |
|
| 10 |
#include "mozilla/Maybe.h" |
| 11 |
|
| 12 |
#include <stdint.h> |
| 13 |
|
| 14 |
#include "ds/Nestable.h" |
| 15 |
#include "frontend/AbstractScopePtr.h" |
| 16 |
#include "frontend/NameAnalysisTypes.h" |
| 17 |
#include "frontend/NameCollections.h" |
| 18 |
#include "frontend/Stencil.h" |
| 19 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 20 |
# include "frontend/UsingEmitter.h" |
| 21 |
#endif |
| 22 |
#include "vm/Opcodes.h" // JSOp |
| 23 |
#include "vm/SharedStencil.h" // GCThingIndex |
| 24 |
|
| 25 |
namespace js { |
| 26 |
namespace frontend { |
| 27 |
|
| 28 |
struct BytecodeEmitter; |
| 29 |
class EvalSharedContext; |
| 30 |
class FunctionBox; |
| 31 |
class GlobalSharedContext; |
| 32 |
class ModuleSharedContext; |
| 33 |
class TaggedParserAtomIndex; |
| 34 |
|
| 35 |
// A scope that introduces bindings. |
| 36 |
class EmitterScope : public Nestable<EmitterScope> { |
| 37 |
// The cache of bound names that may be looked up in the |
| 38 |
// scope. Initially populated as the set of names this scope binds. As |
| 39 |
// names are looked up in enclosing scopes, they are cached on the |
| 40 |
// current scope. |
| 41 |
PooledMapPtr<NameLocationMap> nameCache_; |
| 42 |
|
| 43 |
// If this scope's cache does not include free names, such as the |
| 44 |
// global scope, the NameLocation to return. |
| 45 |
mozilla::Maybe<NameLocation> fallbackFreeNameLocation_; |
| 46 |
|
| 47 |
// True if there is a corresponding EnvironmentObject on the environment |
| 48 |
// chain, false if all bindings are stored in frame slots on the stack. |
| 49 |
bool hasEnvironment_; |
| 50 |
|
| 51 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 52 |
mozilla::Maybe<UsingEmitter> usingEmitter_; |
| 53 |
|
| 54 |
private: |
| 55 |
BlockKind blockKind_ = BlockKind::Other; |
| 56 |
#endif |
| 57 |
|
| 58 |
// The number of enclosing environments. Used for error checking. |
| 59 |
uint8_t environmentChainLength_; |
| 60 |
|
| 61 |
// The next usable slot on the frame for not-closed over bindings. |
| 62 |
// |
| 63 |
// The initial frame slot when assigning slots to bindings is the |
| 64 |
// enclosing scope's nextFrameSlot. For the first scope in a frame, |
| 65 |
// the initial frame slot is 0. |
| 66 |
uint32_t nextFrameSlot_; |
| 67 |
|
| 68 |
// The index in the BytecodeEmitter's interned scope vector, otherwise |
| 69 |
// ScopeNote::NoScopeIndex. |
| 70 |
GCThingIndex scopeIndex_; |
| 71 |
|
| 72 |
// If kind is Lexical, Catch, or With, the index in the BytecodeEmitter's |
| 73 |
// block scope note list. Otherwise ScopeNote::NoScopeNote. |
| 74 |
uint32_t noteIndex_; |
| 75 |
|
| 76 |
[[nodiscard]] bool ensureCache(BytecodeEmitter* bce); |
| 77 |
|
| 78 |
[[nodiscard]] bool checkSlotLimits(BytecodeEmitter* bce, |
| 79 |
const ParserBindingIter& bi); |
| 80 |
|
| 81 |
[[nodiscard]] bool checkEnvironmentChainLength(BytecodeEmitter* bce); |
| 82 |
|
| 83 |
void updateFrameFixedSlots(BytecodeEmitter* bce, const ParserBindingIter& bi); |
| 84 |
|
| 85 |
[[nodiscard]] bool putNameInCache(BytecodeEmitter* bce, |
| 86 |
TaggedParserAtomIndex name, |
| 87 |
NameLocation loc); |
| 88 |
|
| 89 |
mozilla::Maybe<NameLocation> lookupInCache(BytecodeEmitter* bce, |
| 90 |
TaggedParserAtomIndex name); |
| 91 |
|
| 92 |
EmitterScope* enclosing(BytecodeEmitter** bce) const; |
| 93 |
|
| 94 |
mozilla::Maybe<ScopeIndex> enclosingScopeIndex(BytecodeEmitter* bce) const; |
| 95 |
|
| 96 |
static bool nameCanBeFree(BytecodeEmitter* bce, TaggedParserAtomIndex name); |
| 97 |
|
| 98 |
NameLocation searchAndCache(BytecodeEmitter* bce, TaggedParserAtomIndex name); |
| 99 |
|
| 100 |
[[nodiscard]] bool internEmptyGlobalScopeAsBody(BytecodeEmitter* bce); |
| 101 |
|
| 102 |
[[nodiscard]] bool internScopeStencil(BytecodeEmitter* bce, ScopeIndex index); |
| 103 |
|
| 104 |
[[nodiscard]] bool internBodyScopeStencil(BytecodeEmitter* bce, |
| 105 |
ScopeIndex index); |
| 106 |
[[nodiscard]] bool appendScopeNote(BytecodeEmitter* bce); |
| 107 |
|
| 108 |
[[nodiscard]] bool clearFrameSlotRange(BytecodeEmitter* bce, JSOp opcode, |
| 109 |
uint32_t slotStart, |
| 110 |
uint32_t slotEnd) const; |
| 111 |
|
| 112 |
[[nodiscard]] bool deadZoneFrameSlotRange(BytecodeEmitter* bce, |
| 113 |
uint32_t slotStart, |
| 114 |
uint32_t slotEnd) const { |
| 115 |
return clearFrameSlotRange(bce, JSOp::Uninitialized, slotStart, slotEnd); |
| 116 |
} |
| 117 |
|
| 118 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 119 |
void setHasDisposables(BytecodeEmitter* bce) { |
| 120 |
if (!usingEmitter_.isSome()) { |
| 121 |
usingEmitter_.emplace(bce); |
| 122 |
} |
| 123 |
} |
| 124 |
#endif |
| 125 |
|
| 126 |
public: |
| 127 |
explicit EmitterScope(BytecodeEmitter* bce); |
| 128 |
|
| 129 |
void dump(BytecodeEmitter* bce); |
| 130 |
|
| 131 |
[[nodiscard]] bool enterLexical(BytecodeEmitter* bce, ScopeKind kind, |
| 132 |
LexicalScope::ParserData* bindings |
| 133 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 134 |
, |
| 135 |
BlockKind blockKind = BlockKind::Other |
| 136 |
#endif |
| 137 |
); |
| 138 |
[[nodiscard]] bool enterClassBody(BytecodeEmitter* bce, ScopeKind kind, |
| 139 |
ClassBodyScope::ParserData* bindings); |
| 140 |
[[nodiscard]] bool enterNamedLambda(BytecodeEmitter* bce, |
| 141 |
FunctionBox* funbox); |
| 142 |
[[nodiscard]] bool enterFunction(BytecodeEmitter* bce, FunctionBox* funbox); |
| 143 |
[[nodiscard]] bool enterFunctionExtraBodyVar(BytecodeEmitter* bce, |
| 144 |
FunctionBox* funbox); |
| 145 |
[[nodiscard]] bool enterGlobal(BytecodeEmitter* bce, |
| 146 |
GlobalSharedContext* globalsc); |
| 147 |
[[nodiscard]] bool enterEval(BytecodeEmitter* bce, EvalSharedContext* evalsc); |
| 148 |
[[nodiscard]] bool enterModule(BytecodeEmitter* module, |
| 149 |
ModuleSharedContext* modulesc); |
| 150 |
[[nodiscard]] bool enterWith(BytecodeEmitter* bce); |
| 151 |
[[nodiscard]] bool deadZoneFrameSlots(BytecodeEmitter* bce) const; |
| 152 |
|
| 153 |
[[nodiscard]] bool leave(BytecodeEmitter* bce, bool nonLocal = false); |
| 154 |
|
| 155 |
GCThingIndex index() const { |
| 156 |
MOZ_ASSERT(scopeIndex_ != ScopeNote::NoScopeIndex, |
| 157 |
"Did you forget to intern a Scope?"); |
| 158 |
return scopeIndex_; |
| 159 |
} |
| 160 |
|
| 161 |
uint32_t noteIndex() const { return noteIndex_; } |
| 162 |
|
| 163 |
AbstractScopePtr scope(const BytecodeEmitter* bce) const; |
| 164 |
mozilla::Maybe<ScopeIndex> scopeIndex(const BytecodeEmitter* bce) const; |
| 165 |
|
| 166 |
bool hasEnvironment() const { return hasEnvironment_; } |
| 167 |
|
| 168 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 169 |
private: |
| 170 |
// Disposable Scope here refers to any scope |
| 171 |
// with using bindings in it for now that is |
| 172 |
// a lexical scope and a module scope. |
| 173 |
[[nodiscard]] bool prepareForDisposableScopeBody(BytecodeEmitter* bce); |
| 174 |
|
| 175 |
[[nodiscard]] bool emitDisposableScopeBodyEnd(BytecodeEmitter* bce); |
| 176 |
|
| 177 |
public: |
| 178 |
[[nodiscard]] bool prepareForModuleDisposableScopeBody(BytecodeEmitter* bce); |
| 179 |
|
| 180 |
[[nodiscard]] bool emitModuleDisposableScopeBodyEnd(BytecodeEmitter* bce); |
| 181 |
|
| 182 |
[[nodiscard]] bool prepareForDisposableAssignment(UsingHint hint); |
| 183 |
|
| 184 |
bool hasDisposables() const { return usingEmitter_.isSome(); } |
| 185 |
|
| 186 |
bool hasAsyncDisposables() const { |
| 187 |
return hasDisposables() && usingEmitter_->hasAwaitUsing(); |
| 188 |
} |
| 189 |
#endif |
| 190 |
|
| 191 |
// The first frame slot used. |
| 192 |
uint32_t frameSlotStart() const { |
| 193 |
if (EmitterScope* inFrame = enclosingInFrame()) { |
| 194 |
return inFrame->nextFrameSlot_; |
| 195 |
} |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
// The last frame slot used + 1. |
| 200 |
uint32_t frameSlotEnd() const { return nextFrameSlot_; } |
| 201 |
|
| 202 |
EmitterScope* enclosingInFrame() const { |
| 203 |
return Nestable<EmitterScope>::enclosing(); |
| 204 |
} |
| 205 |
|
| 206 |
NameLocation lookup(BytecodeEmitter* bce, TaggedParserAtomIndex name); |
| 207 |
|
| 208 |
// Find both the slot associated with a private name and the location of the |
| 209 |
// corresponding `.privateBrand` binding. |
| 210 |
// |
| 211 |
// Simply doing two separate lookups, one for `name` and another for |
| 212 |
// `.privateBrand`, would give the wrong answer in this case: |
| 213 |
// |
| 214 |
// class Outer { |
| 215 |
// #outerMethod() { reutrn "ok"; } |
| 216 |
// |
| 217 |
// test() { |
| 218 |
// class Inner { |
| 219 |
// #innerMethod() {} |
| 220 |
// test(outer) { |
| 221 |
// return outer.#outerMethod(); |
| 222 |
// } |
| 223 |
// } |
| 224 |
// return new Inner().test(this); |
| 225 |
// } |
| 226 |
// } |
| 227 |
// |
| 228 |
// new Outer().test(); // should return "ok" |
| 229 |
// |
| 230 |
// At the point in Inner.test where `#outerMethod` is called, we need to |
| 231 |
// check for the private brand of `Outer`, not `Inner`; but both class bodies |
| 232 |
// have `.privateBrand` bindings. In a normal `lookup`, the inner binding |
| 233 |
// would shadow the outer one. |
| 234 |
// |
| 235 |
// This method instead sets `brandLoc` to the location of the `.privateBrand` |
| 236 |
// binding in the same class body as the private name `name`, ignoring |
| 237 |
// shadowing. If `name` refers to a name that is actually stamped onto the |
| 238 |
// target object (anything other than a non-static private method), then |
| 239 |
// `brandLoc` is set to Nothing. |
| 240 |
void lookupPrivate(BytecodeEmitter* bce, TaggedParserAtomIndex name, |
| 241 |
NameLocation& loc, mozilla::Maybe<NameLocation>& brandLoc); |
| 242 |
|
| 243 |
mozilla::Maybe<NameLocation> locationBoundInScope(TaggedParserAtomIndex name, |
| 244 |
EmitterScope* target); |
| 245 |
|
| 246 |
// For a given emitter scope, return the number of enclosing environments in |
| 247 |
// the current compilation (this excludes environments that could enclose the |
| 248 |
// compilation, like would happen for an eval copmilation). |
| 249 |
static uint32_t CountEnclosingCompilationEnvironments( |
| 250 |
BytecodeEmitter* bce, EmitterScope* emitterScope); |
| 251 |
}; |
| 252 |
|
| 253 |
} /* namespace frontend */ |
| 254 |
} /* namespace js */ |
| 255 |
|
| 256 |
#endif /* frontend_EmitterScope_h */ |