| 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_FullParseHandler_h |
| 8 |
#define frontend_FullParseHandler_h |
| 9 |
|
| 10 |
#include "mozilla/Maybe.h" // mozilla::Maybe |
| 11 |
#include "mozilla/Result.h" // mozilla::Result, mozilla::UnusedZero |
| 12 |
#include "mozilla/Try.h" // MOZ_TRY* |
| 13 |
|
| 14 |
#include <cstddef> // std::nullptr_t |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
#include "jstypes.h" |
| 18 |
|
| 19 |
#include "frontend/CompilationStencil.h" // CompilationState |
| 20 |
#include "frontend/FunctionSyntaxKind.h" // FunctionSyntaxKind |
| 21 |
#include "frontend/NameAnalysisTypes.h" // PrivateNameKind |
| 22 |
#include "frontend/ParseNode.h" |
| 23 |
#include "frontend/Parser-macros.h" // MOZ_TRY_VAR_OR_RETURN |
| 24 |
#include "frontend/ParserAtom.h" // TaggedParserAtomIndex |
| 25 |
#include "frontend/SharedContext.h" |
| 26 |
#include "frontend/Stencil.h" |
| 27 |
|
| 28 |
template <> |
| 29 |
struct mozilla::detail::UnusedZero<js::frontend::ParseNode*> { |
| 30 |
static const bool value = true; |
| 31 |
}; |
| 32 |
|
| 33 |
#define DEFINE_UNUSED_ZERO(typeName) \ |
| 34 |
template <> \ |
| 35 |
struct mozilla::detail::UnusedZero<js::frontend::typeName*> { \ |
| 36 |
static const bool value = true; \ |
| 37 |
}; |
| 38 |
FOR_EACH_PARSENODE_SUBCLASS(DEFINE_UNUSED_ZERO) |
| 39 |
#undef DEFINE_UNUSED_ZERO |
| 40 |
|
| 41 |
namespace js { |
| 42 |
namespace frontend { |
| 43 |
|
| 44 |
class TokenStreamAnyChars; |
| 45 |
|
| 46 |
// Parse handler used when generating a full parse tree for all code which the |
| 47 |
// parser encounters. |
| 48 |
class FullParseHandler { |
| 49 |
ParseNodeAllocator allocator; |
| 50 |
|
| 51 |
ParseNode* allocParseNode(size_t size) { |
| 52 |
return static_cast<ParseNode*>(allocator.allocNode(size)); |
| 53 |
} |
| 54 |
|
| 55 |
// If this is a full parse to construct the bytecode for a function that |
| 56 |
// was previously lazily parsed, we still don't want to full parse the |
| 57 |
// inner functions. These members are used for this functionality: |
| 58 |
// |
| 59 |
// - reuseGCThings if ture it means that the following fields are valid. |
| 60 |
// - gcThingsData holds an incomplete stencil-like copy of inner functions as |
| 61 |
// well as atoms. |
| 62 |
// - scriptData and scriptExtra_ hold information necessary to locate inner |
| 63 |
// functions to skip over each. |
| 64 |
// - lazyInnerFunctionIndex is used as we skip over inner functions |
| 65 |
// (see skipLazyInnerFunction), |
| 66 |
// - lazyClosedOverBindingIndex is used to synchronize binding computation |
| 67 |
// with the scope traversal. |
| 68 |
// (see propagateFreeNamesAndMarkClosedOverBindings), |
| 69 |
const CompilationSyntaxParseCache& previousParseCache_; |
| 70 |
|
| 71 |
size_t lazyInnerFunctionIndex; |
| 72 |
size_t lazyClosedOverBindingIndex; |
| 73 |
|
| 74 |
bool reuseGCThings; |
| 75 |
|
| 76 |
/* new_ methods for creating parse nodes. These report OOM on context. */ |
| 77 |
JS_DECLARE_NEW_METHODS(new_, allocParseNode, inline) |
| 78 |
|
| 79 |
public: |
| 80 |
using NodeError = ParseNodeError; |
| 81 |
|
| 82 |
using Node = ParseNode*; |
| 83 |
using NodeResult = ParseNodeResult; |
| 84 |
using NodeErrorResult = mozilla::GenericErrorResult<NodeError>; |
| 85 |
|
| 86 |
#define DECLARE_TYPE(typeName) \ |
| 87 |
using typeName##Type = typeName*; \ |
| 88 |
using typeName##Result = mozilla::Result<typeName*, NodeError>; |
| 89 |
FOR_EACH_PARSENODE_SUBCLASS(DECLARE_TYPE) |
| 90 |
#undef DECLARE_TYPE |
| 91 |
|
| 92 |
template <class T, typename... Args> |
| 93 |
inline mozilla::Result<T*, NodeError> newResult(Args&&... args) { |
| 94 |
auto* node = new_<T>(std::forward<Args>(args)...); |
| 95 |
if (!node) { |
| 96 |
return mozilla::Result<T*, NodeError>(NodeError()); |
| 97 |
} |
| 98 |
return node; |
| 99 |
} |
| 100 |
|
| 101 |
using NullNode = std::nullptr_t; |
| 102 |
|
| 103 |
bool isPropertyOrPrivateMemberAccess(Node node) { |
| 104 |
return node->isKind(ParseNodeKind::DotExpr) || |
| 105 |
node->isKind(ParseNodeKind::ElemExpr) || |
| 106 |
node->isKind(ParseNodeKind::PrivateMemberExpr) || |
| 107 |
node->isKind(ParseNodeKind::ArgumentsLength); |
| 108 |
} |
| 109 |
|
| 110 |
bool isOptionalPropertyOrPrivateMemberAccess(Node node) { |
| 111 |
return node->isKind(ParseNodeKind::OptionalDotExpr) || |
| 112 |
node->isKind(ParseNodeKind::OptionalElemExpr) || |
| 113 |
node->isKind(ParseNodeKind::PrivateMemberExpr); |
| 114 |
} |
| 115 |
|
| 116 |
bool isFunctionCall(Node node) { |
| 117 |
// Note: super() is a special form, *not* a function call. |
| 118 |
return node->isKind(ParseNodeKind::CallExpr); |
| 119 |
} |
| 120 |
|
| 121 |
static bool isUnparenthesizedDestructuringPattern(Node node) { |
| 122 |
return !node->isInParens() && (node->isKind(ParseNodeKind::ObjectExpr) || |
| 123 |
node->isKind(ParseNodeKind::ArrayExpr)); |
| 124 |
} |
| 125 |
|
| 126 |
static bool isParenthesizedDestructuringPattern(Node node) { |
| 127 |
// Technically this isn't a destructuring pattern at all -- the grammar |
| 128 |
// doesn't treat it as such. But we need to know when this happens to |
| 129 |
// consider it a SyntaxError rather than an invalid-left-hand-side |
| 130 |
// ReferenceError. |
| 131 |
return node->isInParens() && (node->isKind(ParseNodeKind::ObjectExpr) || |
| 132 |
node->isKind(ParseNodeKind::ArrayExpr)); |
| 133 |
} |
| 134 |
|
| 135 |
FullParseHandler(FrontendContext* fc, CompilationState& compilationState) |
| 136 |
: allocator(fc, compilationState.parserAllocScope.alloc()), |
| 137 |
previousParseCache_(compilationState.previousParseCache), |
| 138 |
lazyInnerFunctionIndex(0), |
| 139 |
lazyClosedOverBindingIndex(0), |
| 140 |
reuseGCThings(compilationState.input.isDelazifying()) {} |
| 141 |
|
| 142 |
static NullNode null() { return NullNode(); } |
| 143 |
static constexpr NodeErrorResult errorResult() { |
| 144 |
return NodeErrorResult(NodeError()); |
| 145 |
} |
| 146 |
|
| 147 |
#define DECLARE_AS(typeName) \ |
| 148 |
static typeName##Type as##typeName(Node node) { \ |
| 149 |
return &node->as<typeName>(); \ |
| 150 |
} |
| 151 |
FOR_EACH_PARSENODE_SUBCLASS(DECLARE_AS) |
| 152 |
#undef DECLARE_AS |
| 153 |
|
| 154 |
NameNodeResult newName(TaggedParserAtomIndex name, const TokenPos& pos) { |
| 155 |
return newResult<NameNode>(ParseNodeKind::Name, name, pos); |
| 156 |
} |
| 157 |
|
| 158 |
UnaryNodeResult newComputedName(Node expr, uint32_t begin, uint32_t end) { |
| 159 |
TokenPos pos(begin, end); |
| 160 |
return newResult<UnaryNode>(ParseNodeKind::ComputedName, pos, expr); |
| 161 |
} |
| 162 |
|
| 163 |
UnaryNodeResult newSyntheticComputedName(Node expr, uint32_t begin, |
| 164 |
uint32_t end) { |
| 165 |
TokenPos pos(begin, end); |
| 166 |
UnaryNode* node; |
| 167 |
MOZ_TRY_VAR(node, |
| 168 |
newResult<UnaryNode>(ParseNodeKind::ComputedName, pos, expr)); |
| 169 |
node->setSyntheticComputedName(); |
| 170 |
return node; |
| 171 |
} |
| 172 |
|
| 173 |
NameNodeResult newObjectLiteralPropertyName(TaggedParserAtomIndex atom, |
| 174 |
const TokenPos& pos) { |
| 175 |
return newResult<NameNode>(ParseNodeKind::ObjectPropertyName, atom, pos); |
| 176 |
} |
| 177 |
|
| 178 |
NameNodeResult newPrivateName(TaggedParserAtomIndex atom, |
| 179 |
const TokenPos& pos) { |
| 180 |
return newResult<NameNode>(ParseNodeKind::PrivateName, atom, pos); |
| 181 |
} |
| 182 |
|
| 183 |
NumericLiteralResult newNumber(double value, DecimalPoint decimalPoint, |
| 184 |
const TokenPos& pos) { |
| 185 |
return newResult<NumericLiteral>(value, decimalPoint, pos); |
| 186 |
} |
| 187 |
|
| 188 |
BigIntLiteralResult newBigInt(BigIntIndex index, const TokenPos& pos) { |
| 189 |
return newResult<BigIntLiteral>(index, pos); |
| 190 |
} |
| 191 |
|
| 192 |
BooleanLiteralResult newBooleanLiteral(bool cond, const TokenPos& pos) { |
| 193 |
return newResult<BooleanLiteral>(cond, pos); |
| 194 |
} |
| 195 |
|
| 196 |
NameNodeResult newStringLiteral(TaggedParserAtomIndex atom, |
| 197 |
const TokenPos& pos) { |
| 198 |
return newResult<NameNode>(ParseNodeKind::StringExpr, atom, pos); |
| 199 |
} |
| 200 |
|
| 201 |
NameNodeResult newTemplateStringLiteral(TaggedParserAtomIndex atom, |
| 202 |
const TokenPos& pos) { |
| 203 |
return newResult<NameNode>(ParseNodeKind::TemplateStringExpr, atom, pos); |
| 204 |
} |
| 205 |
|
| 206 |
CallSiteNodeResult newCallSiteObject(uint32_t begin) { |
| 207 |
CallSiteNode* callSiteObj; |
| 208 |
MOZ_TRY_VAR(callSiteObj, newResult<CallSiteNode>(begin)); |
| 209 |
|
| 210 |
ListNode* rawNodes; |
| 211 |
MOZ_TRY_VAR(rawNodes, newArrayLiteral(callSiteObj->pn_pos.begin)); |
| 212 |
|
| 213 |
addArrayElement(callSiteObj, rawNodes); |
| 214 |
|
| 215 |
return callSiteObj; |
| 216 |
} |
| 217 |
|
| 218 |
void addToCallSiteObject(CallSiteNodeType callSiteObj, Node rawNode, |
| 219 |
Node cookedNode) { |
| 220 |
MOZ_ASSERT(callSiteObj->isKind(ParseNodeKind::CallSiteObj)); |
| 221 |
MOZ_ASSERT(rawNode->isKind(ParseNodeKind::TemplateStringExpr)); |
| 222 |
MOZ_ASSERT(cookedNode->isKind(ParseNodeKind::TemplateStringExpr) || |
| 223 |
cookedNode->isKind(ParseNodeKind::RawUndefinedExpr)); |
| 224 |
|
| 225 |
addArrayElement(callSiteObj, cookedNode); |
| 226 |
addArrayElement(callSiteObj->rawNodes(), rawNode); |
| 227 |
|
| 228 |
/* |
| 229 |
* We don't know when the last noSubstTemplate will come in, and we |
| 230 |
* don't want to deal with this outside this method |
| 231 |
*/ |
| 232 |
setEndPosition(callSiteObj, callSiteObj->rawNodes()); |
| 233 |
} |
| 234 |
|
| 235 |
ThisLiteralResult newThisLiteral(const TokenPos& pos, Node thisName) { |
| 236 |
return newResult<ThisLiteral>(pos, thisName); |
| 237 |
} |
| 238 |
|
| 239 |
NullLiteralResult newNullLiteral(const TokenPos& pos) { |
| 240 |
return newResult<NullLiteral>(pos); |
| 241 |
} |
| 242 |
|
| 243 |
RawUndefinedLiteralResult newRawUndefinedLiteral(const TokenPos& pos) { |
| 244 |
return newResult<RawUndefinedLiteral>(pos); |
| 245 |
} |
| 246 |
|
| 247 |
RegExpLiteralResult newRegExp(RegExpIndex index, const TokenPos& pos) { |
| 248 |
return newResult<RegExpLiteral>(index, pos); |
| 249 |
} |
| 250 |
|
| 251 |
ConditionalExpressionResult newConditional(Node cond, Node thenExpr, |
| 252 |
Node elseExpr) { |
| 253 |
return newResult<ConditionalExpression>(cond, thenExpr, elseExpr); |
| 254 |
} |
| 255 |
|
| 256 |
UnaryNodeResult newDelete(uint32_t begin, Node expr) { |
| 257 |
if (expr->isKind(ParseNodeKind::Name)) { |
| 258 |
return newUnary(ParseNodeKind::DeleteNameExpr, begin, expr); |
| 259 |
} |
| 260 |
|
| 261 |
if (expr->isKind(ParseNodeKind::DotExpr)) { |
| 262 |
return newUnary(ParseNodeKind::DeletePropExpr, begin, expr); |
| 263 |
} |
| 264 |
|
| 265 |
if (expr->isKind(ParseNodeKind::ElemExpr)) { |
| 266 |
return newUnary(ParseNodeKind::DeleteElemExpr, begin, expr); |
| 267 |
} |
| 268 |
|
| 269 |
if (expr->isKind(ParseNodeKind::OptionalChain)) { |
| 270 |
Node kid = expr->as<UnaryNode>().kid(); |
| 271 |
// Handle property deletion explicitly. OptionalCall is handled |
| 272 |
// via DeleteExpr. |
| 273 |
if (kid->isKind(ParseNodeKind::DotExpr) || |
| 274 |
kid->isKind(ParseNodeKind::OptionalDotExpr) || |
| 275 |
kid->isKind(ParseNodeKind::ElemExpr) || |
| 276 |
kid->isKind(ParseNodeKind::OptionalElemExpr)) { |
| 277 |
return newUnary(ParseNodeKind::DeleteOptionalChainExpr, begin, kid); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
return newUnary(ParseNodeKind::DeleteExpr, begin, expr); |
| 282 |
} |
| 283 |
|
| 284 |
UnaryNodeResult newTypeof(uint32_t begin, Node kid) { |
| 285 |
ParseNodeKind pnk = kid->isKind(ParseNodeKind::Name) |
| 286 |
? ParseNodeKind::TypeOfNameExpr |
| 287 |
: ParseNodeKind::TypeOfExpr; |
| 288 |
return newUnary(pnk, begin, kid); |
| 289 |
} |
| 290 |
|
| 291 |
UnaryNodeResult newUnary(ParseNodeKind kind, uint32_t begin, Node kid) { |
| 292 |
TokenPos pos(begin, kid->pn_pos.end); |
| 293 |
return newResult<UnaryNode>(kind, pos, kid); |
| 294 |
} |
| 295 |
|
| 296 |
UnaryNodeResult newUpdate(ParseNodeKind kind, uint32_t begin, Node kid) { |
| 297 |
TokenPos pos(begin, kid->pn_pos.end); |
| 298 |
return newResult<UnaryNode>(kind, pos, kid); |
| 299 |
} |
| 300 |
|
| 301 |
UnaryNodeResult newSpread(uint32_t begin, Node kid) { |
| 302 |
TokenPos pos(begin, kid->pn_pos.end); |
| 303 |
return newResult<UnaryNode>(ParseNodeKind::Spread, pos, kid); |
| 304 |
} |
| 305 |
|
| 306 |
private: |
| 307 |
BinaryNodeResult newBinary(ParseNodeKind kind, Node left, Node right) { |
| 308 |
TokenPos pos(left->pn_pos.begin, right->pn_pos.end); |
| 309 |
return newResult<BinaryNode>(kind, pos, left, right); |
| 310 |
} |
| 311 |
|
| 312 |
public: |
| 313 |
NodeResult appendOrCreateList(ParseNodeKind kind, Node left, Node right, |
| 314 |
ParseContext* pc) { |
| 315 |
return ParseNode::appendOrCreateList(kind, left, right, this, pc); |
| 316 |
} |
| 317 |
|
| 318 |
// Expressions |
| 319 |
|
| 320 |
ListNodeResult newArrayLiteral(uint32_t begin) { |
| 321 |
return newResult<ListNode>(ParseNodeKind::ArrayExpr, |
| 322 |
TokenPos(begin, begin + 1)); |
| 323 |
} |
| 324 |
|
| 325 |
[[nodiscard]] bool addElision(ListNodeType literal, const TokenPos& pos) { |
| 326 |
MOZ_ASSERT(literal->isKind(ParseNodeKind::ArrayExpr)); |
| 327 |
|
| 328 |
NullaryNode* elision; |
| 329 |
MOZ_TRY_VAR_OR_RETURN( |
| 330 |
elision, newResult<NullaryNode>(ParseNodeKind::Elision, pos), false); |
| 331 |
addList(/* list = */ literal, /* kid = */ elision); |
| 332 |
literal->setHasNonConstInitializer(); |
| 333 |
return true; |
| 334 |
} |
| 335 |
|
| 336 |
[[nodiscard]] bool addSpreadElement(ListNodeType literal, uint32_t begin, |
| 337 |
Node inner) { |
| 338 |
MOZ_ASSERT(literal->isKind(ParseNodeKind::ArrayExpr)); |
| 339 |
|
| 340 |
UnaryNodeType spread; |
| 341 |
MOZ_TRY_VAR_OR_RETURN(spread, newSpread(begin, inner), false); |
| 342 |
addList(/* list = */ literal, /* kid = */ spread); |
| 343 |
literal->setHasNonConstInitializer(); |
| 344 |
return true; |
| 345 |
} |
| 346 |
|
| 347 |
void addArrayElement(ListNodeType literal, Node element) { |
| 348 |
MOZ_ASSERT(literal->isKind(ParseNodeKind::ArrayExpr) || |
| 349 |
literal->isKind(ParseNodeKind::CallSiteObj)); |
| 350 |
if (!element->isConstant()) { |
| 351 |
literal->setHasNonConstInitializer(); |
| 352 |
} |
| 353 |
addList(/* list = */ literal, /* kid = */ element); |
| 354 |
} |
| 355 |
|
| 356 |
CallNodeResult newCall(Node callee, ListNodeType args, JSOp callOp) { |
| 357 |
return newResult<CallNode>(ParseNodeKind::CallExpr, callOp, callee, args); |
| 358 |
} |
| 359 |
|
| 360 |
CallNodeResult newOptionalCall(Node callee, ListNodeType args, JSOp callOp) { |
| 361 |
return newResult<CallNode>(ParseNodeKind::OptionalCallExpr, callOp, callee, |
| 362 |
args); |
| 363 |
} |
| 364 |
|
| 365 |
ListNodeResult newArguments(const TokenPos& pos) { |
| 366 |
return newResult<ListNode>(ParseNodeKind::Arguments, pos); |
| 367 |
} |
| 368 |
|
| 369 |
CallNodeResult newSuperCall(Node callee, ListNodeType args, bool isSpread) { |
| 370 |
return newResult<CallNode>( |
| 371 |
ParseNodeKind::SuperCallExpr, |
| 372 |
isSpread ? JSOp::SpreadSuperCall : JSOp::SuperCall, callee, args); |
| 373 |
} |
| 374 |
|
| 375 |
CallNodeResult newTaggedTemplate(Node tag, ListNodeType args, JSOp callOp) { |
| 376 |
return newResult<CallNode>(ParseNodeKind::TaggedTemplateExpr, callOp, tag, |
| 377 |
args); |
| 378 |
} |
| 379 |
|
| 380 |
ListNodeResult newObjectLiteral(uint32_t begin) { |
| 381 |
return newResult<ListNode>(ParseNodeKind::ObjectExpr, |
| 382 |
TokenPos(begin, begin + 1)); |
| 383 |
} |
| 384 |
|
| 385 |
ClassNodeResult newClass(Node name, Node heritage, |
| 386 |
LexicalScopeNodeType memberBlock, |
| 387 |
#ifdef ENABLE_DECORATORS |
| 388 |
ListNodeType decorators, |
| 389 |
FunctionNodeType addInitializerFunction, |
| 390 |
#endif |
| 391 |
const TokenPos& pos) { |
| 392 |
return newResult<ClassNode>(name, heritage, memberBlock, |
| 393 |
#ifdef ENABLE_DECORATORS |
| 394 |
decorators, addInitializerFunction, |
| 395 |
#endif |
| 396 |
pos); |
| 397 |
} |
| 398 |
ListNodeResult newClassMemberList(uint32_t begin) { |
| 399 |
return newResult<ListNode>(ParseNodeKind::ClassMemberList, |
| 400 |
TokenPos(begin, begin + 1)); |
| 401 |
} |
| 402 |
ClassNamesResult newClassNames(Node outer, Node inner, const TokenPos& pos) { |
| 403 |
return newResult<ClassNames>(outer, inner, pos); |
| 404 |
} |
| 405 |
NewTargetNodeResult newNewTarget(NullaryNodeType newHolder, |
| 406 |
NullaryNodeType targetHolder, |
| 407 |
NameNodeType newTargetName) { |
| 408 |
return newResult<NewTargetNode>(newHolder, targetHolder, newTargetName); |
| 409 |
} |
| 410 |
NullaryNodeResult newPosHolder(const TokenPos& pos) { |
| 411 |
return newResult<NullaryNode>(ParseNodeKind::PosHolder, pos); |
| 412 |
} |
| 413 |
UnaryNodeResult newSuperBase(Node thisName, const TokenPos& pos) { |
| 414 |
return newResult<UnaryNode>(ParseNodeKind::SuperBase, pos, thisName); |
| 415 |
} |
| 416 |
[[nodiscard]] bool addPrototypeMutation(ListNodeType literal, uint32_t begin, |
| 417 |
Node expr) { |
| 418 |
MOZ_ASSERT(literal->isKind(ParseNodeKind::ObjectExpr)); |
| 419 |
|
| 420 |
// Object literals with mutated [[Prototype]] are non-constant so that |
| 421 |
// singleton objects will have Object.prototype as their [[Prototype]]. |
| 422 |
literal->setHasNonConstInitializer(); |
| 423 |
|
| 424 |
UnaryNode* mutation; |
| 425 |
MOZ_TRY_VAR_OR_RETURN( |
| 426 |
mutation, newUnary(ParseNodeKind::MutateProto, begin, expr), false); |
| 427 |
addList(/* list = */ literal, /* kid = */ mutation); |
| 428 |
return true; |
| 429 |
} |
| 430 |
|
| 431 |
BinaryNodeResult newPropertyDefinition(Node key, Node val) { |
| 432 |
MOZ_ASSERT(isUsableAsObjectPropertyName(key)); |
| 433 |
checkAndSetIsDirectRHSAnonFunction(val); |
| 434 |
return newResult<PropertyDefinition>(key, val, AccessorType::None); |
| 435 |
} |
| 436 |
|
| 437 |
void addPropertyDefinition(ListNodeType literal, BinaryNodeType propdef) { |
| 438 |
MOZ_ASSERT(literal->isKind(ParseNodeKind::ObjectExpr)); |
| 439 |
MOZ_ASSERT(propdef->isKind(ParseNodeKind::PropertyDefinition)); |
| 440 |
|
| 441 |
if (!propdef->right()->isConstant()) { |
| 442 |
literal->setHasNonConstInitializer(); |
| 443 |
} |
| 444 |
|
| 445 |
addList(/* list = */ literal, /* kid = */ propdef); |
| 446 |
} |
| 447 |
|
| 448 |
[[nodiscard]] bool addPropertyDefinition(ListNodeType literal, Node key, |
| 449 |
Node val) { |
| 450 |
BinaryNode* propdef; |
| 451 |
MOZ_TRY_VAR_OR_RETURN(propdef, newPropertyDefinition(key, val), false); |
| 452 |
addPropertyDefinition(literal, propdef); |
| 453 |
return true; |
| 454 |
} |
| 455 |
|
| 456 |
[[nodiscard]] bool addShorthand(ListNodeType literal, NameNodeType name, |
| 457 |
NameNodeType expr) { |
| 458 |
MOZ_ASSERT(literal->isKind(ParseNodeKind::ObjectExpr)); |
| 459 |
MOZ_ASSERT(name->isKind(ParseNodeKind::ObjectPropertyName)); |
| 460 |
MOZ_ASSERT(expr->isKind(ParseNodeKind::Name)); |
| 461 |
MOZ_ASSERT(name->atom() == expr->atom()); |
| 462 |
|
| 463 |
literal->setHasNonConstInitializer(); |
| 464 |
BinaryNode* propdef; |
| 465 |
MOZ_TRY_VAR_OR_RETURN( |
| 466 |
propdef, newBinary(ParseNodeKind::Shorthand, name, expr), false); |
| 467 |
addList(/* list = */ literal, /* kid = */ propdef); |
| 468 |
return true; |
| 469 |
} |
| 470 |
|
| 471 |
[[nodiscard]] bool addSpreadProperty(ListNodeType literal, uint32_t begin, |
| 472 |
Node inner) { |
| 473 |
MOZ_ASSERT(literal->isKind(ParseNodeKind::ObjectExpr)); |
| 474 |
|
| 475 |
literal->setHasNonConstInitializer(); |
| 476 |
ParseNode* spread; |
| 477 |
MOZ_TRY_VAR_OR_RETURN(spread, newSpread(begin, inner), false); |
| 478 |
addList(/* list = */ literal, /* kid = */ spread); |
| 479 |
return true; |
| 480 |
} |
| 481 |
|
| 482 |
[[nodiscard]] bool addObjectMethodDefinition(ListNodeType literal, Node key, |
| 483 |
FunctionNodeType funNode, |
| 484 |
AccessorType atype) { |
| 485 |
literal->setHasNonConstInitializer(); |
| 486 |
|
| 487 |
checkAndSetIsDirectRHSAnonFunction(funNode); |
| 488 |
|
| 489 |
ParseNode* propdef; |
| 490 |
MOZ_TRY_VAR_OR_RETURN( |
| 491 |
propdef, newObjectMethodOrPropertyDefinition(key, funNode, atype), |
| 492 |
false); |
| 493 |
addList(/* list = */ literal, /* kid = */ propdef); |
| 494 |
return true; |
| 495 |
} |
| 496 |
|
| 497 |
[[nodiscard]] ClassMethodResult newDefaultClassConstructor( |
| 498 |
Node key, FunctionNodeType funNode) { |
| 499 |
MOZ_ASSERT(isUsableAsObjectPropertyName(key)); |
| 500 |
|
| 501 |
checkAndSetIsDirectRHSAnonFunction(funNode); |
| 502 |
|
| 503 |
return newResult<ClassMethod>( |
| 504 |
ParseNodeKind::DefaultConstructor, key, funNode, AccessorType::None, |
| 505 |
/* isStatic = */ false, /* initializeIfPrivate = */ nullptr |
| 506 |
#ifdef ENABLE_DECORATORS |
| 507 |
, |
| 508 |
/* decorators = */ nullptr |
| 509 |
#endif |
| 510 |
); |
| 511 |
} |
| 512 |
|
| 513 |
[[nodiscard]] ClassMethodResult newClassMethodDefinition( |
| 514 |
Node key, FunctionNodeType funNode, AccessorType atype, bool isStatic, |
| 515 |
mozilla::Maybe<FunctionNodeType> initializerIfPrivate |
| 516 |
#ifdef ENABLE_DECORATORS |
| 517 |
, |
| 518 |
ListNodeType decorators |
| 519 |
#endif |
| 520 |
) { |
| 521 |
MOZ_ASSERT(isUsableAsObjectPropertyName(key)); |
| 522 |
|
| 523 |
checkAndSetIsDirectRHSAnonFunction(funNode); |
| 524 |
|
| 525 |
if (initializerIfPrivate.isSome()) { |
| 526 |
return newResult<ClassMethod>(ParseNodeKind::ClassMethod, key, funNode, |
| 527 |
atype, isStatic, |
| 528 |
initializerIfPrivate.value() |
| 529 |
#ifdef ENABLE_DECORATORS |
| 530 |
, |
| 531 |
decorators |
| 532 |
#endif |
| 533 |
); |
| 534 |
} |
| 535 |
return newResult<ClassMethod>(ParseNodeKind::ClassMethod, key, funNode, |
| 536 |
atype, isStatic, |
| 537 |
/* initializeIfPrivate = */ nullptr |
| 538 |
#ifdef ENABLE_DECORATORS |
| 539 |
, |
| 540 |
decorators |
| 541 |
#endif |
| 542 |
); |
| 543 |
} |
| 544 |
|
| 545 |
[[nodiscard]] ClassFieldResult newClassFieldDefinition( |
| 546 |
Node name, FunctionNodeType initializer, bool isStatic |
| 547 |
#ifdef ENABLE_DECORATORS |
| 548 |
, |
| 549 |
ListNodeType decorators, ClassMethodType accessorGetterNode, |
| 550 |
ClassMethodType accessorSetterNode |
| 551 |
#endif |
| 552 |
) { |
| 553 |
MOZ_ASSERT(isUsableAsObjectPropertyName(name)); |
| 554 |
|
| 555 |
return newResult<ClassField>(name, initializer, isStatic |
| 556 |
#if ENABLE_DECORATORS |
| 557 |
, |
| 558 |
decorators, accessorGetterNode, |
| 559 |
accessorSetterNode |
| 560 |
#endif |
| 561 |
); |
| 562 |
} |
| 563 |
|
| 564 |
[[nodiscard]] StaticClassBlockResult newStaticClassBlock( |
| 565 |
FunctionNodeType block) { |
| 566 |
return newResult<StaticClassBlock>(block); |
| 567 |
} |
| 568 |
|
| 569 |
[[nodiscard]] bool addClassMemberDefinition(ListNodeType memberList, |
| 570 |
Node member) { |
| 571 |
MOZ_ASSERT(memberList->isKind(ParseNodeKind::ClassMemberList)); |
| 572 |
// Constructors can be surrounded by LexicalScopes. |
| 573 |
MOZ_ASSERT(member->isKind(ParseNodeKind::DefaultConstructor) || |
| 574 |
member->isKind(ParseNodeKind::ClassMethod) || |
| 575 |
member->isKind(ParseNodeKind::ClassField) || |
| 576 |
member->isKind(ParseNodeKind::StaticClassBlock) || |
| 577 |
(member->isKind(ParseNodeKind::LexicalScope) && |
| 578 |
member->as<LexicalScopeNode>().scopeBody()->is<ClassMethod>())); |
| 579 |
|
| 580 |
addList(/* list = */ memberList, /* kid = */ member); |
| 581 |
return true; |
| 582 |
} |
| 583 |
|
| 584 |
UnaryNodeResult newInitialYieldExpression(uint32_t begin, Node gen) { |
| 585 |
TokenPos pos(begin, begin + 1); |
| 586 |
return newResult<UnaryNode>(ParseNodeKind::InitialYield, pos, gen); |
| 587 |
} |
| 588 |
|
| 589 |
UnaryNodeResult newYieldExpression(uint32_t begin, Node value) { |
| 590 |
TokenPos pos(begin, value ? value->pn_pos.end : begin + 1); |
| 591 |
return newResult<UnaryNode>(ParseNodeKind::YieldExpr, pos, value); |
| 592 |
} |
| 593 |
|
| 594 |
UnaryNodeResult newYieldStarExpression(uint32_t begin, Node value) { |
| 595 |
TokenPos pos(begin, value->pn_pos.end); |
| 596 |
return newResult<UnaryNode>(ParseNodeKind::YieldStarExpr, pos, value); |
| 597 |
} |
| 598 |
|
| 599 |
UnaryNodeResult newAwaitExpression(uint32_t begin, Node value) { |
| 600 |
TokenPos pos(begin, value ? value->pn_pos.end : begin + 1); |
| 601 |
return newResult<UnaryNode>(ParseNodeKind::AwaitExpr, pos, value); |
| 602 |
} |
| 603 |
|
| 604 |
UnaryNodeResult newOptionalChain(uint32_t begin, Node value) { |
| 605 |
TokenPos pos(begin, value->pn_pos.end); |
| 606 |
return newResult<UnaryNode>(ParseNodeKind::OptionalChain, pos, value); |
| 607 |
} |
| 608 |
|
| 609 |
// Statements |
| 610 |
|
| 611 |
ListNodeResult newStatementList(const TokenPos& pos) { |
| 612 |
return newResult<ListNode>(ParseNodeKind::StatementList, pos); |
| 613 |
} |
| 614 |
|
| 615 |
[[nodiscard]] bool isFunctionStmt(Node stmt) { |
| 616 |
while (stmt->isKind(ParseNodeKind::LabelStmt)) { |
| 617 |
stmt = stmt->as<LabeledStatement>().statement(); |
| 618 |
} |
| 619 |
return stmt->is<FunctionNode>(); |
| 620 |
} |
| 621 |
|
| 622 |
void addStatementToList(ListNodeType list, Node stmt) { |
| 623 |
MOZ_ASSERT(list->isKind(ParseNodeKind::StatementList)); |
| 624 |
|
| 625 |
addList(/* list = */ list, /* kid = */ stmt); |
| 626 |
|
| 627 |
if (isFunctionStmt(stmt)) { |
| 628 |
// Notify the emitter that the block contains body-level function |
| 629 |
// definitions that should be processed before the rest of nodes. |
| 630 |
list->setHasTopLevelFunctionDeclarations(); |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
void setListEndPosition(ListNodeType list, const TokenPos& pos) { |
| 635 |
MOZ_ASSERT(list->isKind(ParseNodeKind::StatementList)); |
| 636 |
list->pn_pos.end = pos.end; |
| 637 |
} |
| 638 |
|
| 639 |
void addCaseStatementToList(ListNodeType list, CaseClauseType caseClause) { |
| 640 |
MOZ_ASSERT(list->isKind(ParseNodeKind::StatementList)); |
| 641 |
|
| 642 |
addList(/* list = */ list, /* kid = */ caseClause); |
| 643 |
|
| 644 |
if (caseClause->statementList()->hasTopLevelFunctionDeclarations()) { |
| 645 |
list->setHasTopLevelFunctionDeclarations(); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
[[nodiscard]] bool prependInitialYield(ListNodeType stmtList, Node genName) { |
| 650 |
MOZ_ASSERT(stmtList->isKind(ParseNodeKind::StatementList)); |
| 651 |
|
| 652 |
TokenPos yieldPos(stmtList->pn_pos.begin, stmtList->pn_pos.begin + 1); |
| 653 |
NullaryNode* makeGen; |
| 654 |
MOZ_TRY_VAR_OR_RETURN( |
| 655 |
makeGen, newResult<NullaryNode>(ParseNodeKind::Generator, yieldPos), |
| 656 |
false); |
| 657 |
|
| 658 |
ParseNode* genInit; |
| 659 |
MOZ_TRY_VAR_OR_RETURN( |
| 660 |
genInit, |
| 661 |
newAssignment(ParseNodeKind::AssignExpr, /* lhs = */ genName, |
| 662 |
/* rhs = */ makeGen), |
| 663 |
false); |
| 664 |
|
| 665 |
UnaryNode* initialYield; |
| 666 |
MOZ_TRY_VAR_OR_RETURN(initialYield, |
| 667 |
newInitialYieldExpression(yieldPos.begin, genInit), |
| 668 |
false); |
| 669 |
|
| 670 |
stmtList->prepend(initialYield); |
| 671 |
return true; |
| 672 |
} |
| 673 |
|
| 674 |
BinaryNodeResult newSetThis(Node thisName, Node value) { |
| 675 |
return newBinary(ParseNodeKind::SetThis, thisName, value); |
| 676 |
} |
| 677 |
|
| 678 |
NullaryNodeResult newEmptyStatement(const TokenPos& pos) { |
| 679 |
return newResult<NullaryNode>(ParseNodeKind::EmptyStmt, pos); |
| 680 |
} |
| 681 |
|
| 682 |
BinaryNodeResult newImportAttribute(Node keyNode, Node valueNode) { |
| 683 |
return newBinary(ParseNodeKind::ImportAttribute, keyNode, valueNode); |
| 684 |
} |
| 685 |
|
| 686 |
BinaryNodeResult newModuleRequest(Node moduleSpec, Node importAttributeList, |
| 687 |
const TokenPos& pos) { |
| 688 |
return newResult<BinaryNode>(ParseNodeKind::ImportModuleRequest, pos, |
| 689 |
moduleSpec, importAttributeList); |
| 690 |
} |
| 691 |
|
| 692 |
BinaryNodeResult newImportDeclaration(Node importSpecSet, Node moduleRequest, |
| 693 |
const TokenPos& pos) { |
| 694 |
return newResult<BinaryNode>(ParseNodeKind::ImportDecl, pos, importSpecSet, |
| 695 |
moduleRequest); |
| 696 |
} |
| 697 |
|
| 698 |
BinaryNodeResult newImportSpec(Node importNameNode, Node bindingName) { |
| 699 |
return newBinary(ParseNodeKind::ImportSpec, importNameNode, bindingName); |
| 700 |
} |
| 701 |
|
| 702 |
UnaryNodeResult newImportNamespaceSpec(uint32_t begin, Node bindingName) { |
| 703 |
return newUnary(ParseNodeKind::ImportNamespaceSpec, begin, bindingName); |
| 704 |
} |
| 705 |
|
| 706 |
UnaryNodeResult newExportDeclaration(Node kid, const TokenPos& pos) { |
| 707 |
return newResult<UnaryNode>(ParseNodeKind::ExportStmt, pos, kid); |
| 708 |
} |
| 709 |
|
| 710 |
BinaryNodeResult newExportFromDeclaration(uint32_t begin, Node exportSpecSet, |
| 711 |
Node moduleRequest) { |
| 712 |
BinaryNode* decl; |
| 713 |
MOZ_TRY_VAR(decl, newResult<BinaryNode>(ParseNodeKind::ExportFromStmt, |
| 714 |
exportSpecSet, moduleRequest)); |
| 715 |
decl->pn_pos.begin = begin; |
| 716 |
return decl; |
| 717 |
} |
| 718 |
|
| 719 |
BinaryNodeResult newExportDefaultDeclaration(Node kid, Node maybeBinding, |
| 720 |
const TokenPos& pos) { |
| 721 |
if (maybeBinding) { |
| 722 |
MOZ_ASSERT(maybeBinding->isKind(ParseNodeKind::Name)); |
| 723 |
MOZ_ASSERT(!maybeBinding->isInParens()); |
| 724 |
|
| 725 |
checkAndSetIsDirectRHSAnonFunction(kid); |
| 726 |
} |
| 727 |
|
| 728 |
return newResult<BinaryNode>(ParseNodeKind::ExportDefaultStmt, pos, kid, |
| 729 |
maybeBinding); |
| 730 |
} |
| 731 |
|
| 732 |
BinaryNodeResult newExportSpec(Node bindingName, Node exportName) { |
| 733 |
return newBinary(ParseNodeKind::ExportSpec, bindingName, exportName); |
| 734 |
} |
| 735 |
|
| 736 |
UnaryNodeResult newExportNamespaceSpec(uint32_t begin, Node exportName) { |
| 737 |
return newUnary(ParseNodeKind::ExportNamespaceSpec, begin, exportName); |
| 738 |
} |
| 739 |
|
| 740 |
NullaryNodeResult newExportBatchSpec(const TokenPos& pos) { |
| 741 |
return newResult<NullaryNode>(ParseNodeKind::ExportBatchSpecStmt, pos); |
| 742 |
} |
| 743 |
|
| 744 |
BinaryNodeResult newImportMeta(NullaryNodeType importHolder, |
| 745 |
NullaryNodeType metaHolder) { |
| 746 |
return newResult<BinaryNode>(ParseNodeKind::ImportMetaExpr, importHolder, |
| 747 |
metaHolder); |
| 748 |
} |
| 749 |
|
| 750 |
BinaryNodeResult newCallImport(NullaryNodeType importHolder, Node singleArg) { |
| 751 |
return newResult<BinaryNode>(ParseNodeKind::CallImportExpr, importHolder, |
| 752 |
singleArg); |
| 753 |
} |
| 754 |
|
| 755 |
BinaryNodeResult newCallImportSpec(Node specifierArg, Node optionalArg) { |
| 756 |
return newResult<BinaryNode>(ParseNodeKind::CallImportSpec, specifierArg, |
| 757 |
optionalArg); |
| 758 |
} |
| 759 |
|
| 760 |
UnaryNodeResult newExprStatement(Node expr, uint32_t end) { |
| 761 |
MOZ_ASSERT(expr->pn_pos.end <= end); |
| 762 |
return newResult<UnaryNode>(ParseNodeKind::ExpressionStmt, |
| 763 |
TokenPos(expr->pn_pos.begin, end), expr); |
| 764 |
} |
| 765 |
|
| 766 |
TernaryNodeResult newIfStatement(uint32_t begin, Node cond, Node thenBranch, |
| 767 |
Node elseBranch) { |
| 768 |
TernaryNode* node; |
| 769 |
MOZ_TRY_VAR(node, newResult<TernaryNode>(ParseNodeKind::IfStmt, cond, |
| 770 |
thenBranch, elseBranch)); |
| 771 |
node->pn_pos.begin = begin; |
| 772 |
return node; |
| 773 |
} |
| 774 |
|
| 775 |
BinaryNodeResult newDoWhileStatement(Node body, Node cond, |
| 776 |
const TokenPos& pos) { |
| 777 |
return newResult<BinaryNode>(ParseNodeKind::DoWhileStmt, pos, body, cond); |
| 778 |
} |
| 779 |
|
| 780 |
BinaryNodeResult newWhileStatement(uint32_t begin, Node cond, Node body) { |
| 781 |
TokenPos pos(begin, body->pn_pos.end); |
| 782 |
return newResult<BinaryNode>(ParseNodeKind::WhileStmt, pos, cond, body); |
| 783 |
} |
| 784 |
|
| 785 |
ForNodeResult newForStatement(uint32_t begin, TernaryNodeType forHead, |
| 786 |
Node body, unsigned iflags) { |
| 787 |
return newResult<ForNode>(TokenPos(begin, body->pn_pos.end), forHead, body, |
| 788 |
iflags); |
| 789 |
} |
| 790 |
|
| 791 |
TernaryNodeResult newForHead(Node init, Node test, Node update, |
| 792 |
const TokenPos& pos) { |
| 793 |
return newResult<TernaryNode>(ParseNodeKind::ForHead, init, test, update, |
| 794 |
pos); |
| 795 |
} |
| 796 |
|
| 797 |
TernaryNodeResult newForInOrOfHead(ParseNodeKind kind, Node target, |
| 798 |
Node iteratedExpr, const TokenPos& pos) { |
| 799 |
MOZ_ASSERT(kind == ParseNodeKind::ForIn || kind == ParseNodeKind::ForOf); |
| 800 |
return newResult<TernaryNode>(kind, target, nullptr, iteratedExpr, pos); |
| 801 |
} |
| 802 |
|
| 803 |
SwitchStatementResult newSwitchStatement( |
| 804 |
uint32_t begin, Node discriminant, |
| 805 |
LexicalScopeNodeType lexicalForCaseList, bool hasDefault) { |
| 806 |
return newResult<SwitchStatement>(begin, discriminant, lexicalForCaseList, |
| 807 |
hasDefault); |
| 808 |
} |
| 809 |
|
| 810 |
CaseClauseResult newCaseOrDefault(uint32_t begin, Node expr, Node body) { |
| 811 |
return newResult<CaseClause>(expr, body, begin); |
| 812 |
} |
| 813 |
|
| 814 |
ContinueStatementResult newContinueStatement(TaggedParserAtomIndex label, |
| 815 |
const TokenPos& pos) { |
| 816 |
return newResult<ContinueStatement>(label, pos); |
| 817 |
} |
| 818 |
|
| 819 |
BreakStatementResult newBreakStatement(TaggedParserAtomIndex label, |
| 820 |
const TokenPos& pos) { |
| 821 |
return newResult<BreakStatement>(label, pos); |
| 822 |
} |
| 823 |
|
| 824 |
UnaryNodeResult newReturnStatement(Node expr, const TokenPos& pos) { |
| 825 |
MOZ_ASSERT_IF(expr, pos.encloses(expr->pn_pos)); |
| 826 |
return newResult<UnaryNode>(ParseNodeKind::ReturnStmt, pos, expr); |
| 827 |
} |
| 828 |
|
| 829 |
UnaryNodeResult newExpressionBody(Node expr) { |
| 830 |
return newResult<UnaryNode>(ParseNodeKind::ReturnStmt, expr->pn_pos, expr); |
| 831 |
} |
| 832 |
|
| 833 |
BinaryNodeResult newWithStatement(uint32_t begin, Node expr, Node body) { |
| 834 |
return newResult<BinaryNode>(ParseNodeKind::WithStmt, |
| 835 |
TokenPos(begin, body->pn_pos.end), expr, body); |
| 836 |
} |
| 837 |
|
| 838 |
LabeledStatementResult newLabeledStatement(TaggedParserAtomIndex label, |
| 839 |
Node stmt, uint32_t begin) { |
| 840 |
return newResult<LabeledStatement>(label, stmt, begin); |
| 841 |
} |
| 842 |
|
| 843 |
UnaryNodeResult newThrowStatement(Node expr, const TokenPos& pos) { |
| 844 |
MOZ_ASSERT(pos.encloses(expr->pn_pos)); |
| 845 |
return newResult<UnaryNode>(ParseNodeKind::ThrowStmt, pos, expr); |
| 846 |
} |
| 847 |
|
| 848 |
TernaryNodeResult newTryStatement(uint32_t begin, Node body, |
| 849 |
LexicalScopeNodeType catchScope, |
| 850 |
Node finallyBlock) { |
| 851 |
return newResult<TryNode>(begin, body, catchScope, finallyBlock); |
| 852 |
} |
| 853 |
|
| 854 |
DebuggerStatementResult newDebuggerStatement(const TokenPos& pos) { |
| 855 |
return newResult<DebuggerStatement>(pos); |
| 856 |
} |
| 857 |
|
| 858 |
NameNodeResult newPropertyName(TaggedParserAtomIndex name, |
| 859 |
const TokenPos& pos) { |
| 860 |
return newResult<NameNode>(ParseNodeKind::PropertyNameExpr, name, pos); |
| 861 |
} |
| 862 |
|
| 863 |
PropertyAccessResult newPropertyAccess(Node expr, NameNodeType key) { |
| 864 |
return newResult<PropertyAccess>(expr, key, expr->pn_pos.begin, |
| 865 |
key->pn_pos.end); |
| 866 |
} |
| 867 |
|
| 868 |
ArgumentsLengthResult newArgumentsLength(Node expr, NameNodeType key) { |
| 869 |
return newResult<ArgumentsLength>(expr, key, expr->pn_pos.begin, |
| 870 |
key->pn_pos.end); |
| 871 |
} |
| 872 |
|
| 873 |
PropertyByValueResult newPropertyByValue(Node lhs, Node index, uint32_t end) { |
| 874 |
return newResult<PropertyByValue>(lhs, index, lhs->pn_pos.begin, end); |
| 875 |
} |
| 876 |
|
| 877 |
OptionalPropertyAccessResult newOptionalPropertyAccess(Node expr, |
| 878 |
NameNodeType key) { |
| 879 |
return newResult<OptionalPropertyAccess>(expr, key, expr->pn_pos.begin, |
| 880 |
key->pn_pos.end); |
| 881 |
} |
| 882 |
|
| 883 |
OptionalPropertyByValueResult newOptionalPropertyByValue(Node lhs, Node index, |
| 884 |
uint32_t end) { |
| 885 |
return newResult<OptionalPropertyByValue>(lhs, index, lhs->pn_pos.begin, |
| 886 |
end); |
| 887 |
} |
| 888 |
|
| 889 |
PrivateMemberAccessResult newPrivateMemberAccess(Node lhs, |
| 890 |
NameNodeType privateName, |
| 891 |
uint32_t end) { |
| 892 |
return newResult<PrivateMemberAccess>(lhs, privateName, lhs->pn_pos.begin, |
| 893 |
end); |
| 894 |
} |
| 895 |
|
| 896 |
OptionalPrivateMemberAccessResult newOptionalPrivateMemberAccess( |
| 897 |
Node lhs, NameNodeType privateName, uint32_t end) { |
| 898 |
return newResult<OptionalPrivateMemberAccess>(lhs, privateName, |
| 899 |
lhs->pn_pos.begin, end); |
| 900 |
} |
| 901 |
|
| 902 |
bool setupCatchScope(LexicalScopeNodeType lexicalScope, Node catchName, |
| 903 |
Node catchBody) { |
| 904 |
BinaryNode* catchClause; |
| 905 |
if (catchName) { |
| 906 |
MOZ_TRY_VAR_OR_RETURN( |
| 907 |
catchClause, |
| 908 |
newResult<BinaryNode>(ParseNodeKind::Catch, catchName, catchBody), |
| 909 |
false); |
| 910 |
} else { |
| 911 |
MOZ_TRY_VAR_OR_RETURN( |
| 912 |
catchClause, |
| 913 |
newResult<BinaryNode>(ParseNodeKind::Catch, catchBody->pn_pos, |
| 914 |
catchName, catchBody), |
| 915 |
false); |
| 916 |
} |
| 917 |
lexicalScope->setScopeBody(catchClause); |
| 918 |
return true; |
| 919 |
} |
| 920 |
|
| 921 |
[[nodiscard]] inline bool setLastFunctionFormalParameterDefault( |
| 922 |
FunctionNodeType funNode, Node defaultValue); |
| 923 |
|
| 924 |
void checkAndSetIsDirectRHSAnonFunction(Node pn) { |
| 925 |
if (IsAnonymousFunctionDefinition(pn)) { |
| 926 |
pn->setDirectRHSAnonFunction(true); |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
ParamsBodyNodeResult newParamsBody(const TokenPos& pos) { |
| 931 |
return newResult<ParamsBodyNode>(pos); |
| 932 |
} |
| 933 |
|
| 934 |
FunctionNodeResult newFunction(FunctionSyntaxKind syntaxKind, |
| 935 |
const TokenPos& pos) { |
| 936 |
return newResult<FunctionNode>(syntaxKind, pos); |
| 937 |
} |
| 938 |
|
| 939 |
BinaryNodeResult newObjectMethodOrPropertyDefinition(Node key, Node value, |
| 940 |
AccessorType atype) { |
| 941 |
MOZ_ASSERT(isUsableAsObjectPropertyName(key)); |
| 942 |
|
| 943 |
return newResult<PropertyDefinition>(key, value, atype); |
| 944 |
} |
| 945 |
|
| 946 |
void setFunctionFormalParametersAndBody(FunctionNodeType funNode, |
| 947 |
ParamsBodyNodeType paramsBody) { |
| 948 |
funNode->setBody(paramsBody); |
| 949 |
} |
| 950 |
void setFunctionBox(FunctionNodeType funNode, FunctionBox* funbox) { |
| 951 |
funNode->setFunbox(funbox); |
| 952 |
funbox->functionNode = funNode; |
| 953 |
} |
| 954 |
void addFunctionFormalParameter(FunctionNodeType funNode, Node argpn) { |
| 955 |
addList(/* list = */ funNode->body(), /* kid = */ argpn); |
| 956 |
} |
| 957 |
void setFunctionBody(FunctionNodeType funNode, LexicalScopeNodeType body) { |
| 958 |
addList(/* list = */ funNode->body(), /* kid = */ body); |
| 959 |
} |
| 960 |
|
| 961 |
ModuleNodeResult newModule(const TokenPos& pos) { |
| 962 |
return newResult<ModuleNode>(pos); |
| 963 |
} |
| 964 |
|
| 965 |
LexicalScopeNodeResult newLexicalScope(LexicalScope::ParserData* bindings, |
| 966 |
Node body, |
| 967 |
ScopeKind kind = ScopeKind::Lexical) { |
| 968 |
return newResult<LexicalScopeNode>(bindings, body, kind); |
| 969 |
} |
| 970 |
|
| 971 |
ClassBodyScopeNodeResult newClassBodyScope( |
| 972 |
ClassBodyScope::ParserData* bindings, ListNodeType body) { |
| 973 |
return newResult<ClassBodyScopeNode>(bindings, body); |
| 974 |
} |
| 975 |
|
| 976 |
CallNodeResult newNewExpression(uint32_t begin, Node ctor, ListNodeType args, |
| 977 |
bool isSpread) { |
| 978 |
return newResult<CallNode>(ParseNodeKind::NewExpr, |
| 979 |
isSpread ? JSOp::SpreadNew : JSOp::New, |
| 980 |
TokenPos(begin, args->pn_pos.end), ctor, args); |
| 981 |
} |
| 982 |
|
| 983 |
AssignmentNodeResult newAssignment(ParseNodeKind kind, Node lhs, Node rhs) { |
| 984 |
if ((kind == ParseNodeKind::AssignExpr || |
| 985 |
kind == ParseNodeKind::CoalesceAssignExpr || |
| 986 |
kind == ParseNodeKind::OrAssignExpr || |
| 987 |
kind == ParseNodeKind::AndAssignExpr) && |
| 988 |
lhs->isKind(ParseNodeKind::Name) && !lhs->isInParens()) { |
| 989 |
checkAndSetIsDirectRHSAnonFunction(rhs); |
| 990 |
} |
| 991 |
|
| 992 |
return newResult<AssignmentNode>(kind, lhs, rhs); |
| 993 |
} |
| 994 |
|
| 995 |
BinaryNodeResult newInitExpr(Node lhs, Node rhs) { |
| 996 |
TokenPos pos(lhs->pn_pos.begin, rhs->pn_pos.end); |
| 997 |
return newResult<BinaryNode>(ParseNodeKind::InitExpr, pos, lhs, rhs); |
| 998 |
} |
| 999 |
|
| 1000 |
bool isUnparenthesizedAssignment(Node node) { |
| 1001 |
if ((node->isKind(ParseNodeKind::AssignExpr)) && !node->isInParens()) { |
| 1002 |
return true; |
| 1003 |
} |
| 1004 |
|
| 1005 |
return false; |
| 1006 |
} |
| 1007 |
|
| 1008 |
bool isUnparenthesizedUnaryExpression(Node node) { |
| 1009 |
if (!node->isInParens()) { |
| 1010 |
ParseNodeKind kind = node->getKind(); |
| 1011 |
return kind == ParseNodeKind::VoidExpr || |
| 1012 |
kind == ParseNodeKind::NotExpr || |
| 1013 |
kind == ParseNodeKind::BitNotExpr || |
| 1014 |
kind == ParseNodeKind::PosExpr || kind == ParseNodeKind::NegExpr || |
| 1015 |
kind == ParseNodeKind::AwaitExpr || IsTypeofKind(kind) || |
| 1016 |
IsDeleteKind(kind); |
| 1017 |
} |
| 1018 |
return false; |
| 1019 |
} |
| 1020 |
|
| 1021 |
bool isReturnStatement(Node node) { |
| 1022 |
return node->isKind(ParseNodeKind::ReturnStmt); |
| 1023 |
} |
| 1024 |
|
| 1025 |
bool isStatementPermittedAfterReturnStatement(Node node) { |
| 1026 |
ParseNodeKind kind = node->getKind(); |
| 1027 |
return kind == ParseNodeKind::Function || kind == ParseNodeKind::VarStmt || |
| 1028 |
kind == ParseNodeKind::BreakStmt || |
| 1029 |
kind == ParseNodeKind::ThrowStmt || kind == ParseNodeKind::EmptyStmt; |
| 1030 |
} |
| 1031 |
|
| 1032 |
bool isSuperBase(Node node) { return node->isKind(ParseNodeKind::SuperBase); } |
| 1033 |
|
| 1034 |
bool isUsableAsObjectPropertyName(Node node) { |
| 1035 |
return node->isKind(ParseNodeKind::NumberExpr) || |
| 1036 |
node->isKind(ParseNodeKind::BigIntExpr) || |
| 1037 |
node->isKind(ParseNodeKind::ObjectPropertyName) || |
| 1038 |
node->isKind(ParseNodeKind::StringExpr) || |
| 1039 |
node->isKind(ParseNodeKind::ComputedName) || |
| 1040 |
node->isKind(ParseNodeKind::PrivateName); |
| 1041 |
} |
| 1042 |
|
| 1043 |
AssignmentNodeResult finishInitializerAssignment(NameNodeType nameNode, |
| 1044 |
Node init) { |
| 1045 |
MOZ_ASSERT(nameNode->isKind(ParseNodeKind::Name)); |
| 1046 |
MOZ_ASSERT(!nameNode->isInParens()); |
| 1047 |
|
| 1048 |
checkAndSetIsDirectRHSAnonFunction(init); |
| 1049 |
|
| 1050 |
return newAssignment(ParseNodeKind::AssignExpr, nameNode, init); |
| 1051 |
} |
| 1052 |
|
| 1053 |
void setBeginPosition(Node pn, Node oth) { |
| 1054 |
setBeginPosition(pn, oth->pn_pos.begin); |
| 1055 |
} |
| 1056 |
void setBeginPosition(Node pn, uint32_t begin) { |
| 1057 |
pn->pn_pos.begin = begin; |
| 1058 |
MOZ_ASSERT(pn->pn_pos.begin <= pn->pn_pos.end); |
| 1059 |
} |
| 1060 |
|
| 1061 |
void setEndPosition(Node pn, Node oth) { |
| 1062 |
setEndPosition(pn, oth->pn_pos.end); |
| 1063 |
} |
| 1064 |
void setEndPosition(Node pn, uint32_t end) { |
| 1065 |
pn->pn_pos.end = end; |
| 1066 |
MOZ_ASSERT(pn->pn_pos.begin <= pn->pn_pos.end); |
| 1067 |
} |
| 1068 |
|
| 1069 |
uint32_t getFunctionNameOffset(Node func, TokenStreamAnyChars& ts) { |
| 1070 |
return func->pn_pos.begin; |
| 1071 |
} |
| 1072 |
|
| 1073 |
ListNodeResult newList(ParseNodeKind kind, const TokenPos& pos) { |
| 1074 |
auto list = newResult<ListNode>(kind, pos); |
| 1075 |
MOZ_ASSERT_IF(list.isOk(), !list.unwrap()->is<DeclarationListNode>()); |
| 1076 |
MOZ_ASSERT_IF(list.isOk(), !list.unwrap()->is<ParamsBodyNode>()); |
| 1077 |
return list; |
| 1078 |
} |
| 1079 |
|
| 1080 |
ListNodeResult newList(ParseNodeKind kind, Node kid) { |
| 1081 |
auto list = newResult<ListNode>(kind, kid); |
| 1082 |
MOZ_ASSERT_IF(list.isOk(), !list.unwrap()->is<DeclarationListNode>()); |
| 1083 |
MOZ_ASSERT_IF(list.isOk(), !list.unwrap()->is<ParamsBodyNode>()); |
| 1084 |
return list; |
| 1085 |
} |
| 1086 |
|
| 1087 |
DeclarationListNodeResult newDeclarationList(ParseNodeKind kind, |
| 1088 |
const TokenPos& pos) { |
| 1089 |
return newResult<DeclarationListNode>(kind, pos); |
| 1090 |
} |
| 1091 |
|
| 1092 |
ListNodeResult newCommaExpressionList(Node kid) { |
| 1093 |
return newResult<ListNode>(ParseNodeKind::CommaExpr, kid); |
| 1094 |
} |
| 1095 |
|
| 1096 |
void addList(ListNodeType list, Node kid) { list->append(kid); } |
| 1097 |
|
| 1098 |
void setListHasNonConstInitializer(ListNodeType literal) { |
| 1099 |
literal->setHasNonConstInitializer(); |
| 1100 |
} |
| 1101 |
|
| 1102 |
// NOTE: This is infallible. |
| 1103 |
template <typename NodeType> |
| 1104 |
[[nodiscard]] NodeType parenthesize(NodeType node) { |
| 1105 |
node->setInParens(true); |
| 1106 |
return node; |
| 1107 |
} |
| 1108 |
|
| 1109 |
// NOTE: This is infallible. |
| 1110 |
template <typename NodeType> |
| 1111 |
[[nodiscard]] NodeType setLikelyIIFE(NodeType node) { |
| 1112 |
return parenthesize(node); |
| 1113 |
} |
| 1114 |
|
| 1115 |
bool isName(Node node) { return node->isKind(ParseNodeKind::Name); } |
| 1116 |
|
| 1117 |
bool isArgumentsName(Node node) { |
| 1118 |
return node->isKind(ParseNodeKind::Name) && |
| 1119 |
node->as<NameNode>().atom() == |
| 1120 |
TaggedParserAtomIndex::WellKnown::arguments(); |
| 1121 |
} |
| 1122 |
|
| 1123 |
bool isLengthName(Node node) { |
| 1124 |
return node->isKind(ParseNodeKind::PropertyNameExpr) && |
| 1125 |
node->as<NameNode>().atom() == |
| 1126 |
TaggedParserAtomIndex::WellKnown::length(); |
| 1127 |
} |
| 1128 |
|
| 1129 |
bool isEvalName(Node node) { |
| 1130 |
return node->isKind(ParseNodeKind::Name) && |
| 1131 |
node->as<NameNode>().atom() == |
| 1132 |
TaggedParserAtomIndex::WellKnown::eval(); |
| 1133 |
} |
| 1134 |
|
| 1135 |
bool isAsyncKeyword(Node node) { |
| 1136 |
return node->isKind(ParseNodeKind::Name) && |
| 1137 |
node->pn_pos.begin + strlen("async") == node->pn_pos.end && |
| 1138 |
node->as<NameNode>().atom() == |
| 1139 |
TaggedParserAtomIndex::WellKnown::async(); |
| 1140 |
} |
| 1141 |
|
| 1142 |
bool isArgumentsLength(Node node) { |
| 1143 |
return node->isKind(ParseNodeKind::ArgumentsLength); |
| 1144 |
} |
| 1145 |
|
| 1146 |
bool isPrivateName(Node node) { |
| 1147 |
return node->isKind(ParseNodeKind::PrivateName); |
| 1148 |
} |
| 1149 |
|
| 1150 |
bool isPrivateMemberAccess(Node node) { |
| 1151 |
if (node->isKind(ParseNodeKind::OptionalChain)) { |
| 1152 |
return isPrivateMemberAccess(node->as<UnaryNode>().kid()); |
| 1153 |
} |
| 1154 |
return node->is<PrivateMemberAccessBase>(); |
| 1155 |
} |
| 1156 |
|
| 1157 |
TaggedParserAtomIndex maybeDottedProperty(Node pn) { |
| 1158 |
return pn->is<PropertyAccessBase>() ? pn->as<PropertyAccessBase>().name() |
| 1159 |
: TaggedParserAtomIndex::null(); |
| 1160 |
} |
| 1161 |
TaggedParserAtomIndex isStringExprStatement(Node pn, TokenPos* pos) { |
| 1162 |
if (pn->is<UnaryNode>()) { |
| 1163 |
UnaryNode* unary = &pn->as<UnaryNode>(); |
| 1164 |
if (auto atom = unary->isStringExprStatement()) { |
| 1165 |
*pos = unary->kid()->pn_pos; |
| 1166 |
return atom; |
| 1167 |
} |
| 1168 |
} |
| 1169 |
return TaggedParserAtomIndex::null(); |
| 1170 |
} |
| 1171 |
|
| 1172 |
bool reuseLazyInnerFunctions() { return reuseGCThings; } |
| 1173 |
bool reuseClosedOverBindings() { return reuseGCThings; } |
| 1174 |
bool reuseRegexpSyntaxParse() { return reuseGCThings; } |
| 1175 |
void nextLazyInnerFunction() { lazyInnerFunctionIndex++; } |
| 1176 |
TaggedParserAtomIndex nextLazyClosedOverBinding() { |
| 1177 |
// Trailing nullptrs were elided in PerHandlerParser::finishFunction(). |
| 1178 |
auto closedOverBindings = previousParseCache_.closedOverBindings(); |
| 1179 |
if (lazyClosedOverBindingIndex >= closedOverBindings.Length()) { |
| 1180 |
return TaggedParserAtomIndex::null(); |
| 1181 |
} |
| 1182 |
|
| 1183 |
return closedOverBindings[lazyClosedOverBindingIndex++]; |
| 1184 |
} |
| 1185 |
const ScriptStencil& cachedScriptData() const { |
| 1186 |
// lazyInnerFunctionIndex is incremented with nextLazyInnferFunction before |
| 1187 |
// reading the content, thus we need -1 to access the element that we just |
| 1188 |
// skipped. |
| 1189 |
return previousParseCache_.scriptData(lazyInnerFunctionIndex - 1); |
| 1190 |
} |
| 1191 |
const ScriptStencilExtra& cachedScriptExtra() const { |
| 1192 |
// lazyInnerFunctionIndex is incremented with nextLazyInnferFunction before |
| 1193 |
// reading the content, thus we need -1 to access the element that we just |
| 1194 |
// skipped. |
| 1195 |
return previousParseCache_.scriptExtra(lazyInnerFunctionIndex - 1); |
| 1196 |
} |
| 1197 |
|
| 1198 |
void setPrivateNameKind(Node node, PrivateNameKind kind) { |
| 1199 |
MOZ_ASSERT(node->is<NameNode>()); |
| 1200 |
node->as<NameNode>().setPrivateNameKind(kind); |
| 1201 |
} |
| 1202 |
}; |
| 1203 |
|
| 1204 |
inline bool FullParseHandler::setLastFunctionFormalParameterDefault( |
| 1205 |
FunctionNodeType funNode, Node defaultValue) { |
| 1206 |
ParamsBodyNode* body = funNode->body(); |
| 1207 |
ParseNode* arg = body->last(); |
| 1208 |
ParseNode* pn; |
| 1209 |
MOZ_TRY_VAR_OR_RETURN( |
| 1210 |
pn, newAssignment(ParseNodeKind::AssignExpr, arg, defaultValue), false); |
| 1211 |
|
| 1212 |
body->replaceLast(pn); |
| 1213 |
return true; |
| 1214 |
} |
| 1215 |
|
| 1216 |
} // namespace frontend |
| 1217 |
} // namespace js |
| 1218 |
|
| 1219 |
#endif /* frontend_FullParseHandler_h */ |