1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
//! S2 T6: `visit(CallExpressionNode *)` (SemanticResolver.cpp:1127-1219) —
//! the three "call specials" the C++ resolver folds into one override, plus
//! the static helper one of them needs. A sibling `impl<'bt, 'sc, 'sm, 'ad>
//! SemanticResolver<'bt, 'sc, 'sm, 'ad>` block, split into its own file for
//! the same reason `identifiers.rs`/`declarations.rs`/`expressions.rs`/
//! `statements.rs`/`classes.rs` were (see `identifiers.rs`'s module doc for
//! why a child module sees `mod.rs`'s private fields): `expressions.rs` is
//! already 1.4k lines, and this visit is a self-contained unit with its own
//! rewrite.
//!
//! Ports `SemanticResolver::visit(ESTree::CallExpressionNode *node)`
//! (cpp:1127-1219) and `SemanticResolver::registerLocalEval`
//! (cpp:2865-2873, declared `static` at SemanticResolver.h:503-505).
//!
//! The visit does three unrelated things in sequence, and `visit_node`
//! dispatches `CallExpression` — and ONLY `CallExpression` — here:
//!
//! 1. **Direct `eval()` detection** (cpp:1128-1161). A call whose callee is
//! the bare identifier `eval` is a *direct* eval. Whether it "looks like"
//! the real global `eval` is decided by the binding: unbound, or bound to
//! a global-scope `UndeclaredGlobalProperty`/`GlobalProperty`, means yes.
//! With `eval` enabled (the `Context` default) a `DirectEval` warning is
//! emitted for that case and [`register_local_eval`] runs
//! UNCONDITIONALLY — i.e. also for a *shadowed* `eval`, where `isEval` was
//! false and no warning was produced. That asymmetry is C++'s, ported as
//! written; `LexicalScope::localEval`'s own comment (SemContext.h:252-254)
//! describes the flag as "true if this scope or any descendent scopes have
//! a local eval call", i.e. a conservative marker, but what consumes it is
//! outside sema and outside this port. With `eval` disabled the warning
//! becomes `EvalDisabled` and no scope is marked.
//! 2. **Rewrite #3: `$SHBuiltin.prop(...)` → `SHBuiltin` node**
//! (cpp:1163-1207). See the section below.
//! 3. **The `super()` check** (cpp:1209-1216): a `super()` call is only
//! legal when the nearest non-arrow enclosing function is a *derived*
//! class constructor.
//!
//! ## What is NOT dispatched here
//!
//! `OptionalCallExpression` and `NewExpression` have **no**
//! `SemanticResolver::visit` overload. Verified against the inventory in
//! SemanticResolver.h:200-306: the only call-family overload is `void
//! visit(ESTree::CallExpressionNode *node)` (SemanticResolver.h:269), and
//! `OptionalCallExpressionNode` does not derive from `CallExpressionNode` —
//! ESTree.def:304-319 makes both of them children of the
//! `CallExpressionLike` GROUP (`ESTREE_FIRST(CallExpressionLike, Base)`),
//! i.e. siblings — so that overload is not even viable for it and C++
//! overload resolution picks the catch-all `void visit(ESTree::Node *node) {
//! visitESTreeChildren(*this, node); }` (SemanticResolver.h:191-193). Both
//! kinds therefore belong in `visit_node`'s override-free generic arm;
//! `NewExpression` has been there since S2 T2 and `OptionalCallExpression`
//! joins it here.
//!
//! The visible consequence, and the reason this is worth stating: `eval?.()`
//! and `new eval()` produce **no** direct-eval warning and mark no scope
//! (pinned by `tests/sema_corpus/eval-direct.js`), and `$SHBuiltin.foo?.(1)`
//! is **not** rewritten — so its `$SHBuiltin` identifier survives and becomes
//! an `invalid use of $SHBuiltin` error (pinned by
//! `tests/sema_corpus/error-shbuiltin.js`). Both are C++'s behavior, and
//! `calls-shapes.js` pins the rest of the two kinds' plain resolution.
//!
//! ## Rewrite #3: `$SHBuiltin.prop(...)` (spec §3.4)
//!
//! C++ mutates the callee in place:
//!
//! ```text
//! auto *shBuiltin = new (astContext_) ESTree::SHBuiltinNode();
//! shBuiltin->copyLocationFrom(methodCallee->_object);
//! methodCallee->_object = shBuiltin;
//! ```
//!
//! `methodCallee` IS `node->_callee`, so after that assignment the same
//! `CallExpressionNode` the visit was handed has a `MemberExpression` callee
//! whose `_object` is an `SHBuiltinNode`, and `visitESTreeChildren(*this,
//! node)` at cpp:1218 walks *that*. Structural fields are immutable here, so
//! the port rebuilds instead: a new `MemberExpression` (through the
//! generated builder, so `computed` — a `Cell` decoration — is carried
//! over) and then a new `CallExpression` around it. Everything after the
//! rewrite point — the property-name branches, the `super()` check and the
//! children walk — runs on the REBUILT node, exactly as C++'s run on the
//! mutated one, and the visit returns `Changed`.
//!
//! Per `resolver/mod.rs`'s "decorate before recursing", note that neither
//! rebuilt node carries any decoration this visit writes: `CallExpression`
//! has no `Cell` fields at all and the `MemberExpression`'s only one
//! (`computed`) is copied by `from_node`. The trap that bit rewrite #1 (a
//! decoration written on the discarded node) therefore cannot arise here —
//! but the rebuild order still matters for a different reason, see below.
//!
//! Three details of the C++ that are load-bearing:
//!
//! - **`resolveIdentifier(ident, false)` runs BEFORE the kind test** and is
//! what makes the rewrite possible: for an unshadowed `$SHBuiltin` it
//! resolves the identifier to the ambient `UndeclaredGlobalProperty` decl
//! (libhermes declares `var $SHBuiltin;`, so it is normally already
//! bound — and if it were not, this call would CREATE it, which is
//! observable in the dump's decl numbering). The decl it returns is what
//! the rewrite is gated on: only `UndeclaredGlobalProperty` rewrites, so
//! `let $SHBuiltin = {}; $SHBuiltin.foo(1)` does not.
//! - **The identifier it resolves is then THROWN AWAY** in the rewriting
//! case, so `setExpressionDecl`'s entry for it is stranded (in C++ too —
//! the node is unlinked from the tree while the side table keeps its
//! entry). Harmless: every consumer, `-dump-sema` included, reaches
//! expression decls by walking the tree, never by iterating that table.
//! - **The rewrite does NOT suppress the `invalid use of $SHBuiltin` error**
//! in `visit(IdentifierNode *, Node *)` (cpp:310-314) — it *avoids* it, by
//! replacing the identifier before the children walk can reach it. In the
//! shadowed case the identifier survives, the walk reaches it and the
//! error fires (pinned by `tests/sema_corpus/error-shbuiltin.js`, which
//! also pins that `resolveIdentifier` being called twice on it — once
//! here, once from the identifier visit — reports the error exactly ONCE,
//! because the second call hits the decl cache).
//!
//! The three module-related property names (`moduleFactory`, `export`,
//! `import`, cpp:1183-1204) are the `$SHBuiltin` CommonJS-module protocol
//! and belong to **S4** (the module visits). They are ported as loud
//! phase-tagged panics rather than approximated. One subtlety recorded here
//! for whoever lands S4: the `export` branch (cpp:1192-1201) visits the
//! children FIRST and only then calls `visitModuleExport(node)`, because the
//! exported name must already be resolved — in this port "visit the
//! children" means `visit_children_mut`, which may hand back a *rebuilt*
//! call node, and it is that rebuilt node `visitModuleExport` must be given
//! (and returned as `Changed`). The `moduleFactory` branch (cpp:1183-1191)
//! and the `export` branch both `return` without falling through to
//! cpp:1218, while the `import` branch (cpp:1202-1204) DOES fall through.
use GCLock;
use ;
use TransformResult;
use ;
use crateScopeId;
use crate;
use copy_location_from;
use SemanticResolver;
/// Mark \p scope and every one of its ancestor scopes as users of local
/// `eval()`. Port of `SemanticResolver::registerLocalEval`
/// (SemanticResolver.cpp:2865-2873).
///
/// C++ declares this `static` (SemanticResolver.h:505) — it touches no
/// resolver state — so it is a free function here rather than a method,
/// taking the `SemContext` the `LexicalScope` records live in. C++'s
/// `LexicalScope *scope` is nullable in principle; `Option<ScopeId>` ports
/// that directly, and the single call site (`curScope_`) is exactly as
/// nullable.
pub
/// The `_name` of a `$SHBuiltin.<prop>` member expression's `_property`,
/// when that property is an identifier at all. Port of
/// `llvh::dyn_cast<ESTree::IdentifierNode>(methodCallee->_property)`
/// (cpp:1171-1172, as of upstream `07efab88d`).
///
/// C++ used to `cast` here, so the enclosing `if (auto *propIdent = ...)` was
/// always taken and a non-`Identifier` `_property` was an assertion failure —
/// which `$SHBuiltin.#x()` inside a class declaring `#x` really did trigger
/// (the `_property` is a `PrivateName`; a non-computed member expression's
/// property can be either). Upstream `07efab88d` ("Fix crash on
/// `$SHBuiltin.#privateName()`") turned it into a `dyn_cast` whose result also
/// gates the whole rewrite, so a private property leaves `$SHBuiltin` alone and
/// the identifier is reported as an `invalid use of $SHBuiltin` when it is
/// visited, like any other non-builtin use. This port mirrors that.