mozjs_sys 0.67.1

System crate for the Mozilla SpiderMonkey JavaScript engine.
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 *
 * Copyright 2015 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef wasmast_h
#define wasmast_h

#include "mozilla/Variant.h"

#include "ds/LifoAlloc.h"
#include "js/HashTable.h"
#include "js/Vector.h"
#include "wasm/WasmTypes.h"

namespace js {
namespace wasm {

const uint32_t AstNoIndex = UINT32_MAX;
const unsigned AST_LIFO_DEFAULT_CHUNK_SIZE = 4096;

/*****************************************************************************/
// wasm AST

class AstExpr;

template <class T>
using AstVector = mozilla::Vector<T, 0, LifoAllocPolicy<Fallible>>;

template <class K, class V, class HP>
using AstHashMap = HashMap<K, V, HP, LifoAllocPolicy<Fallible>>;

using mozilla::Variant;

typedef AstVector<bool> AstBoolVector;

class AstName {
  const char16_t* begin_;
  const char16_t* end_;

 public:
  template <size_t Length>
  explicit AstName(const char16_t (&str)[Length])
      : begin_(str), end_(str + Length - 1) {
    MOZ_ASSERT(str[Length - 1] == u'\0');
  }

  AstName(const char16_t* begin, size_t length)
      : begin_(begin), end_(begin + length) {}
  AstName() : begin_(nullptr), end_(nullptr) {}
  const char16_t* begin() const { return begin_; }
  const char16_t* end() const { return end_; }
  size_t length() const { return end_ - begin_; }
  bool empty() const { return begin_ == nullptr; }

  bool operator==(AstName rhs) const {
    if (length() != rhs.length()) {
      return false;
    }
    if (begin() == rhs.begin()) {
      return true;
    }
    return EqualChars(begin(), rhs.begin(), length());
  }
  bool operator!=(AstName rhs) const { return !(*this == rhs); }
};

class AstRef {
  AstName name_;
  uint32_t index_;

 public:
  AstRef() : index_(AstNoIndex) { MOZ_ASSERT(isInvalid()); }
  explicit AstRef(AstName name) : name_(name), index_(AstNoIndex) {
    MOZ_ASSERT(!isInvalid());
  }
  explicit AstRef(uint32_t index) : index_(index) { MOZ_ASSERT(!isInvalid()); }
  bool isInvalid() const { return name_.empty() && index_ == AstNoIndex; }
  AstName name() const { return name_; }
  bool isIndex() const { return index_ != AstNoIndex; }
  size_t index() const {
    MOZ_ASSERT(index_ != AstNoIndex);
    return index_;
  }
  void setIndex(uint32_t index) {
    MOZ_ASSERT(index_ == AstNoIndex);
    index_ = index;
  }
  bool operator==(AstRef rhs) const {
    return name_ == rhs.name_ && index_ == rhs.index_;
  }
  bool operator!=(AstRef rhs) const { return !(*this == rhs); }
};

class AstValType {
  // When this type is resolved, which_ becomes IsValType.

  enum { IsValType, IsAstRef } which_;
  ValType type_;
  AstRef ref_;

 public:
  AstValType() : which_(IsValType) {}  // type_ is then !isValid()

  explicit AstValType(ValType type) : which_(IsValType), type_(type) {}

  explicit AstValType(AstRef ref) {
    if (ref.name().empty()) {
      which_ = IsValType;
      type_ = ValType(ValType::Ref, ref.index());
    } else {
      which_ = IsAstRef;
      ref_ = ref;
    }
  }

  bool isRefType() const {
    return code() == ValType::AnyRef || code() == ValType::Ref;
  }

  bool isValid() const { return !(which_ == IsValType && !type_.isValid()); }

  bool isResolved() const { return which_ == IsValType; }

  AstRef& asRef() { return ref_; }

  void resolve() {
    MOZ_ASSERT(which_ == IsAstRef);
    which_ = IsValType;
    type_ = ValType(ValType::Ref, ref_.index());
  }

  ValType::Code code() const {
    if (which_ == IsValType) {
      return type_.code();
    }
    return ValType::Ref;
  }

  ValType type() const {
    MOZ_ASSERT(which_ == IsValType);
    return type_;
  }

  bool operator==(const AstValType& that) const {
    if (which_ != that.which_) {
      return false;
    }
    if (which_ == IsValType) {
      return type_ == that.type_;
    }
    return ref_ == that.ref_;
  }

  bool operator!=(const AstValType& that) const { return !(*this == that); }
};

class AstExprType {
  // When this type is resolved, which_ becomes IsExprType.

  enum { IsExprType, IsAstValType } which_;
  union {
    ExprType type_;
    AstValType vt_;
  };

 public:
  MOZ_IMPLICIT AstExprType(ExprType::Code type)
      : which_(IsExprType), type_(type) {}

  MOZ_IMPLICIT AstExprType(ExprType type) : which_(IsExprType), type_(type) {}

  MOZ_IMPLICIT AstExprType(const AstExprType& type) : which_(type.which_) {
    switch (which_) {
      case IsExprType:
        type_ = type.type_;
        break;
      case IsAstValType:
        vt_ = type.vt_;
        break;
    }
  }

  explicit AstExprType(AstValType vt) : which_(IsAstValType), vt_(vt) {}

  bool isVoid() const {
    return which_ == IsExprType && type_ == ExprType::Void;
  }

  bool isResolved() const { return which_ == IsExprType; }

  AstValType& asAstValType() {
    MOZ_ASSERT(which_ == IsAstValType);
    return vt_;
  }

  void resolve() {
    MOZ_ASSERT(which_ == IsAstValType);
    which_ = IsExprType;
    type_ = ExprType(vt_.type());
  }

  ExprType::Code code() const {
    if (which_ == IsExprType) {
      return type_.code();
    }
    return ExprType::Ref;
  }

  ExprType type() const {
    if (which_ == IsExprType) {
      return type_;
    }
    return ExprType(vt_.type());
  }

  bool operator==(const AstExprType& that) const {
    if (which_ != that.which_) {
      return false;
    }
    if (which_ == IsExprType) {
      return type_ == that.type_;
    }
    return vt_ == that.vt_;
  }

  bool operator!=(const AstExprType& that) const { return !(*this == that); }
};

struct AstNameHasher {
  typedef const AstName Lookup;
  static js::HashNumber hash(Lookup l) {
    return mozilla::HashString(l.begin(), l.length());
  }
  static bool match(const AstName key, Lookup lookup) { return key == lookup; }
};

using AstNameMap = AstHashMap<AstName, uint32_t, AstNameHasher>;

typedef AstVector<AstValType> AstValTypeVector;
typedef AstVector<AstExpr*> AstExprVector;
typedef AstVector<AstName> AstNameVector;
typedef AstVector<AstRef> AstRefVector;

struct AstBase {
  void* operator new(size_t numBytes, LifoAlloc& astLifo) throw() {
    return astLifo.alloc(numBytes);
  }
};

struct AstNode {
  void* operator new(size_t numBytes, LifoAlloc& astLifo) throw() {
    return astLifo.alloc(numBytes);
  }
};

class AstFuncType;
class AstStructType;

class AstTypeDef : public AstNode {
 protected:
  enum class Which { IsFuncType, IsStructType };

 private:
  Which which_;

 public:
  explicit AstTypeDef(Which which) : which_(which) {}

  bool isFuncType() const { return which_ == Which::IsFuncType; }
  bool isStructType() const { return which_ == Which::IsStructType; }
  inline AstFuncType& asFuncType();
  inline AstStructType& asStructType();
  inline const AstFuncType& asFuncType() const;
  inline const AstStructType& asStructType() const;
};

class AstFuncType : public AstTypeDef {
  AstName name_;
  AstValTypeVector args_;
  AstExprType ret_;

 public:
  explicit AstFuncType(LifoAlloc& lifo)
      : AstTypeDef(Which::IsFuncType), args_(lifo), ret_(ExprType::Void) {}
  AstFuncType(AstValTypeVector&& args, AstExprType ret)
      : AstTypeDef(Which::IsFuncType), args_(std::move(args)), ret_(ret) {}
  AstFuncType(AstName name, AstFuncType&& rhs)
      : AstTypeDef(Which::IsFuncType),
        name_(name),
        args_(std::move(rhs.args_)),
        ret_(rhs.ret_) {}
  const AstValTypeVector& args() const { return args_; }
  AstValTypeVector& args() { return args_; }
  AstExprType ret() const { return ret_; }
  AstExprType& ret() { return ret_; }
  AstName name() const { return name_; }
  bool operator==(const AstFuncType& rhs) const {
    if (ret() != rhs.ret()) {
      return false;
    }
    size_t len = args().length();
    if (rhs.args().length() != len) {
      return false;
    }
    for (size_t i = 0; i < len; i++) {
      if (args()[i] != rhs.args()[i]) {
        return false;
      }
    }
    return true;
  }

  typedef const AstFuncType& Lookup;
  static HashNumber hash(Lookup ft) {
    HashNumber hn = HashNumber(ft.ret().code());
    for (const AstValType& vt : ft.args()) {
      hn = mozilla::AddToHash(hn, uint32_t(vt.code()));
    }
    return hn;
  }
  static bool match(const AstFuncType* lhs, Lookup rhs) { return *lhs == rhs; }
};

class AstStructType : public AstTypeDef {
  AstName name_;
  AstNameVector fieldNames_;
  AstBoolVector fieldMutability_;
  AstValTypeVector fieldTypes_;

 public:
  explicit AstStructType(LifoAlloc& lifo)
      : AstTypeDef(Which::IsStructType),
        fieldNames_(lifo),
        fieldMutability_(lifo),
        fieldTypes_(lifo) {}
  AstStructType(AstNameVector&& names, AstBoolVector&& mutability,
                AstValTypeVector&& types)
      : AstTypeDef(Which::IsStructType),
        fieldNames_(std::move(names)),
        fieldMutability_(std::move(mutability)),
        fieldTypes_(std::move(types)) {}
  AstStructType(AstName name, AstStructType&& rhs)
      : AstTypeDef(Which::IsStructType),
        name_(name),
        fieldNames_(std::move(rhs.fieldNames_)),
        fieldMutability_(std::move(rhs.fieldMutability_)),
        fieldTypes_(std::move(rhs.fieldTypes_)) {}
  AstName name() const { return name_; }
  const AstNameVector& fieldNames() const { return fieldNames_; }
  const AstBoolVector& fieldMutability() const { return fieldMutability_; }
  const AstValTypeVector& fieldTypes() const { return fieldTypes_; }
  AstValTypeVector& fieldTypes() { return fieldTypes_; }
};

inline AstFuncType& AstTypeDef::asFuncType() {
  MOZ_ASSERT(isFuncType());
  return *static_cast<AstFuncType*>(this);
}

inline AstStructType& AstTypeDef::asStructType() {
  MOZ_ASSERT(isStructType());
  return *static_cast<AstStructType*>(this);
}

inline const AstFuncType& AstTypeDef::asFuncType() const {
  MOZ_ASSERT(isFuncType());
  return *static_cast<const AstFuncType*>(this);
}

inline const AstStructType& AstTypeDef::asStructType() const {
  MOZ_ASSERT(isStructType());
  return *static_cast<const AstStructType*>(this);
}

enum class AstExprKind {
  AtomicCmpXchg,
  AtomicLoad,
  AtomicRMW,
  AtomicStore,
  BinaryOperator,
  Block,
  Branch,
  BranchTable,
  Call,
  CallIndirect,
  ComparisonOperator,
  Const,
  ConversionOperator,
#ifdef ENABLE_WASM_BULKMEM_OPS
  DataOrElemDrop,
#endif
  Drop,
  ExtraConversionOperator,
  First,
  GetGlobal,
  GetLocal,
  If,
  Load,
#ifdef ENABLE_WASM_BULKMEM_OPS
  MemFill,
  MemOrTableCopy,
  MemOrTableInit,
#endif
  MemoryGrow,
  MemorySize,
  Nop,
  Pop,
  RefNull,
  Return,
  SetGlobal,
  SetLocal,
#ifdef ENABLE_WASM_GC
  StructNew,
  StructGet,
  StructSet,
  StructNarrow,
#endif
#ifdef ENABLE_WASM_REFTYPES
  TableGet,
  TableGrow,
  TableSet,
  TableSize,
#endif
  TeeLocal,
  Store,
  TernaryOperator,
  UnaryOperator,
  Unreachable,
  Wait,
  Wake
};

class AstExpr : public AstNode {
  const AstExprKind kind_;
  AstExprType type_;

 protected:
  AstExpr(AstExprKind kind, AstExprType type) : kind_(kind), type_(type) {}

 public:
  AstExprKind kind() const { return kind_; }

  bool isVoid() const { return type_.isVoid(); }

  // Note that for nodes other than blocks and block-like things, the
  // underlying type may be ExprType::Limit for nodes with non-void types.
  AstExprType& type() { return type_; }

  template <class T>
  T& as() {
    MOZ_ASSERT(kind() == T::Kind);
    return static_cast<T&>(*this);
  }
};

struct AstNop : AstExpr {
  static const AstExprKind Kind = AstExprKind::Nop;
  AstNop() : AstExpr(AstExprKind::Nop, ExprType::Void) {}
};

struct AstUnreachable : AstExpr {
  static const AstExprKind Kind = AstExprKind::Unreachable;
  AstUnreachable() : AstExpr(AstExprKind::Unreachable, ExprType::Void) {}
};

class AstDrop : public AstExpr {
  AstExpr& value_;

 public:
  static const AstExprKind Kind = AstExprKind::Drop;
  explicit AstDrop(AstExpr& value)
      : AstExpr(AstExprKind::Drop, ExprType::Void), value_(value) {}
  AstExpr& value() const { return value_; }
};

class AstConst : public AstExpr {
  const LitVal val_;

 public:
  static const AstExprKind Kind = AstExprKind::Const;
  explicit AstConst(LitVal val) : AstExpr(Kind, ExprType::Limit), val_(val) {}
  LitVal val() const { return val_; }
};

class AstGetLocal : public AstExpr {
  AstRef local_;

 public:
  static const AstExprKind Kind = AstExprKind::GetLocal;
  explicit AstGetLocal(AstRef local)
      : AstExpr(Kind, ExprType::Limit), local_(local) {}
  AstRef& local() { return local_; }
};

class AstSetLocal : public AstExpr {
  AstRef local_;
  AstExpr& value_;

 public:
  static const AstExprKind Kind = AstExprKind::SetLocal;
  AstSetLocal(AstRef local, AstExpr& value)
      : AstExpr(Kind, ExprType::Void), local_(local), value_(value) {}
  AstRef& local() { return local_; }
  AstExpr& value() const { return value_; }
};

class AstGetGlobal : public AstExpr {
  AstRef global_;

 public:
  static const AstExprKind Kind = AstExprKind::GetGlobal;
  explicit AstGetGlobal(AstRef global)
      : AstExpr(Kind, ExprType::Limit), global_(global) {}
  AstRef& global() { return global_; }
};

class AstSetGlobal : public AstExpr {
  AstRef global_;
  AstExpr& value_;

 public:
  static const AstExprKind Kind = AstExprKind::SetGlobal;
  AstSetGlobal(AstRef global, AstExpr& value)
      : AstExpr(Kind, ExprType::Void), global_(global), value_(value) {}
  AstRef& global() { return global_; }
  AstExpr& value() const { return value_; }
};

class AstTeeLocal : public AstExpr {
  AstRef local_;
  AstExpr& value_;

 public:
  static const AstExprKind Kind = AstExprKind::TeeLocal;
  AstTeeLocal(AstRef local, AstExpr& value)
      : AstExpr(Kind, ExprType::Limit), local_(local), value_(value) {}
  AstRef& local() { return local_; }
  AstExpr& value() const { return value_; }
};

class AstBlock : public AstExpr {
  Op op_;
  AstName name_;
  AstExprVector exprs_;

 public:
  static const AstExprKind Kind = AstExprKind::Block;
  explicit AstBlock(Op op, AstExprType type, AstName name,
                    AstExprVector&& exprs)
      : AstExpr(Kind, type), op_(op), name_(name), exprs_(std::move(exprs)) {}

  Op op() const { return op_; }
  AstName name() const { return name_; }
  const AstExprVector& exprs() const { return exprs_; }
};

class AstBranch : public AstExpr {
  Op op_;
  AstExpr* cond_;
  AstRef target_;
  AstExpr* value_;

 public:
  static const AstExprKind Kind = AstExprKind::Branch;
  explicit AstBranch(Op op, AstExprType type, AstExpr* cond, AstRef target,
                     AstExpr* value)
      : AstExpr(Kind, type),
        op_(op),
        cond_(cond),
        target_(target),
        value_(value) {}

  Op op() const { return op_; }
  AstRef& target() { return target_; }
  AstExpr& cond() const {
    MOZ_ASSERT(cond_);
    return *cond_;
  }
  AstExpr* maybeValue() const { return value_; }
};

class AstCall : public AstExpr {
  Op op_;
  AstRef func_;
  AstExprVector args_;

 public:
  static const AstExprKind Kind = AstExprKind::Call;
  AstCall(Op op, AstExprType type, AstRef func, AstExprVector&& args)
      : AstExpr(Kind, type), op_(op), func_(func), args_(std::move(args)) {}

  Op op() const { return op_; }
  AstRef& func() { return func_; }
  const AstExprVector& args() const { return args_; }
};

class AstCallIndirect : public AstExpr {
  AstRef targetTable_;
  AstRef funcType_;
  AstExprVector args_;
  AstExpr* index_;

 public:
  static const AstExprKind Kind = AstExprKind::CallIndirect;
  AstCallIndirect(AstRef targetTable, AstRef funcType, AstExprType type,
                  AstExprVector&& args, AstExpr* index)
      : AstExpr(Kind, type),
        targetTable_(targetTable),
        funcType_(funcType),
        args_(std::move(args)),
        index_(index) {}
  AstRef& targetTable() { return targetTable_; }
  AstRef& funcType() { return funcType_; }
  const AstExprVector& args() const { return args_; }
  AstExpr* index() const { return index_; }
};

class AstReturn : public AstExpr {
  AstExpr* maybeExpr_;

 public:
  static const AstExprKind Kind = AstExprKind::Return;
  explicit AstReturn(AstExpr* maybeExpr)
      : AstExpr(Kind, ExprType::Void), maybeExpr_(maybeExpr) {}
  AstExpr* maybeExpr() const { return maybeExpr_; }
};

class AstIf : public AstExpr {
  AstExpr* cond_;
  AstName name_;
  AstExprVector thenExprs_;
  AstExprVector elseExprs_;

 public:
  static const AstExprKind Kind = AstExprKind::If;
  AstIf(AstExprType type, AstExpr* cond, AstName name,
        AstExprVector&& thenExprs, AstExprVector&& elseExprs)
      : AstExpr(Kind, type),
        cond_(cond),
        name_(name),
        thenExprs_(std::move(thenExprs)),
        elseExprs_(std::move(elseExprs)) {}

  AstExpr& cond() const { return *cond_; }
  const AstExprVector& thenExprs() const { return thenExprs_; }
  bool hasElse() const { return elseExprs_.length(); }
  const AstExprVector& elseExprs() const {
    MOZ_ASSERT(hasElse());
    return elseExprs_;
  }
  AstName name() const { return name_; }
};

class AstLoadStoreAddress {
  AstExpr* base_;
  int32_t flags_;
  int32_t offset_;

 public:
  explicit AstLoadStoreAddress(AstExpr* base, int32_t flags, int32_t offset)
      : base_(base), flags_(flags), offset_(offset) {}

  AstExpr& base() const { return *base_; }
  int32_t flags() const { return flags_; }
  int32_t offset() const { return offset_; }
};

class AstLoad : public AstExpr {
  Op op_;
  AstLoadStoreAddress address_;

 public:
  static const AstExprKind Kind = AstExprKind::Load;
  explicit AstLoad(Op op, const AstLoadStoreAddress& address)
      : AstExpr(Kind, ExprType::Limit), op_(op), address_(address) {}

  Op op() const { return op_; }
  const AstLoadStoreAddress& address() const { return address_; }
};

class AstStore : public AstExpr {
  Op op_;
  AstLoadStoreAddress address_;
  AstExpr* value_;

 public:
  static const AstExprKind Kind = AstExprKind::Store;
  explicit AstStore(Op op, const AstLoadStoreAddress& address, AstExpr* value)
      : AstExpr(Kind, ExprType::Void),
        op_(op),
        address_(address),
        value_(value) {}

  Op op() const { return op_; }
  const AstLoadStoreAddress& address() const { return address_; }
  AstExpr& value() const { return *value_; }
};

class AstAtomicCmpXchg : public AstExpr {
  ThreadOp op_;
  AstLoadStoreAddress address_;
  AstExpr* expected_;
  AstExpr* replacement_;

 public:
  static const AstExprKind Kind = AstExprKind::AtomicCmpXchg;
  explicit AstAtomicCmpXchg(ThreadOp op, const AstLoadStoreAddress& address,
                            AstExpr* expected, AstExpr* replacement)
      : AstExpr(Kind, ExprType::Limit),
        op_(op),
        address_(address),
        expected_(expected),
        replacement_(replacement) {}

  ThreadOp op() const { return op_; }
  const AstLoadStoreAddress& address() const { return address_; }
  AstExpr& expected() const { return *expected_; }
  AstExpr& replacement() const { return *replacement_; }
};

class AstAtomicLoad : public AstExpr {
  ThreadOp op_;
  AstLoadStoreAddress address_;

 public:
  static const AstExprKind Kind = AstExprKind::AtomicLoad;
  explicit AstAtomicLoad(ThreadOp op, const AstLoadStoreAddress& address)
      : AstExpr(Kind, ExprType::Limit), op_(op), address_(address) {}

  ThreadOp op() const { return op_; }
  const AstLoadStoreAddress& address() const { return address_; }
};

class AstAtomicRMW : public AstExpr {
  ThreadOp op_;
  AstLoadStoreAddress address_;
  AstExpr* value_;

 public:
  static const AstExprKind Kind = AstExprKind::AtomicRMW;
  explicit AstAtomicRMW(ThreadOp op, const AstLoadStoreAddress& address,
                        AstExpr* value)
      : AstExpr(Kind, ExprType::Limit),
        op_(op),
        address_(address),
        value_(value) {}

  ThreadOp op() const { return op_; }
  const AstLoadStoreAddress& address() const { return address_; }
  AstExpr& value() const { return *value_; }
};

class AstAtomicStore : public AstExpr {
  ThreadOp op_;
  AstLoadStoreAddress address_;
  AstExpr* value_;

 public:
  static const AstExprKind Kind = AstExprKind::AtomicStore;
  explicit AstAtomicStore(ThreadOp op, const AstLoadStoreAddress& address,
                          AstExpr* value)
      : AstExpr(Kind, ExprType::Void),
        op_(op),
        address_(address),
        value_(value) {}

  ThreadOp op() const { return op_; }
  const AstLoadStoreAddress& address() const { return address_; }
  AstExpr& value() const { return *value_; }
};

class AstWait : public AstExpr {
  ThreadOp op_;
  AstLoadStoreAddress address_;
  AstExpr* expected_;
  AstExpr* timeout_;

 public:
  static const AstExprKind Kind = AstExprKind::Wait;
  explicit AstWait(ThreadOp op, const AstLoadStoreAddress& address,
                   AstExpr* expected, AstExpr* timeout)
      : AstExpr(Kind, ExprType::I32),
        op_(op),
        address_(address),
        expected_(expected),
        timeout_(timeout) {}

  ThreadOp op() const { return op_; }
  const AstLoadStoreAddress& address() const { return address_; }
  AstExpr& expected() const { return *expected_; }
  AstExpr& timeout() const { return *timeout_; }
};

class AstWake : public AstExpr {
  AstLoadStoreAddress address_;
  AstExpr* count_;

 public:
  static const AstExprKind Kind = AstExprKind::Wake;
  explicit AstWake(const AstLoadStoreAddress& address, AstExpr* count)
      : AstExpr(Kind, ExprType::I32), address_(address), count_(count) {}

  const AstLoadStoreAddress& address() const { return address_; }
  AstExpr& count() const { return *count_; }
};

#ifdef ENABLE_WASM_BULKMEM_OPS
class AstMemOrTableCopy : public AstExpr {
  bool isMem_;
  AstRef destTable_;
  AstExpr* dest_;
  AstRef srcTable_;
  AstExpr* src_;
  AstExpr* len_;

 public:
  static const AstExprKind Kind = AstExprKind::MemOrTableCopy;
  explicit AstMemOrTableCopy(bool isMem, AstRef destTable, AstExpr* dest,
                             AstRef srcTable, AstExpr* src, AstExpr* len)
      : AstExpr(Kind, ExprType::Void),
        isMem_(isMem),
        destTable_(destTable),
        dest_(dest),
        srcTable_(srcTable),
        src_(src),
        len_(len) {}

  bool isMem() const { return isMem_; }
  AstRef& destTable() {
    MOZ_ASSERT(!isMem());
    return destTable_;
  }
  AstExpr& dest() const { return *dest_; }
  AstRef& srcTable() {
    MOZ_ASSERT(!isMem());
    return srcTable_;
  }
  AstExpr& src() const { return *src_; }
  AstExpr& len() const { return *len_; }
};

class AstDataOrElemDrop : public AstExpr {
  bool isData_;
  uint32_t segIndex_;

 public:
  static const AstExprKind Kind = AstExprKind::DataOrElemDrop;
  explicit AstDataOrElemDrop(bool isData, uint32_t segIndex)
      : AstExpr(Kind, ExprType::Void), isData_(isData), segIndex_(segIndex) {}

  bool isData() const { return isData_; }
  uint32_t segIndex() const { return segIndex_; }
};

class AstMemFill : public AstExpr {
  AstExpr* start_;
  AstExpr* val_;
  AstExpr* len_;

 public:
  static const AstExprKind Kind = AstExprKind::MemFill;
  explicit AstMemFill(AstExpr* start, AstExpr* val, AstExpr* len)
      : AstExpr(Kind, ExprType::Void), start_(start), val_(val), len_(len) {}

  AstExpr& start() const { return *start_; }
  AstExpr& val() const { return *val_; }
  AstExpr& len() const { return *len_; }
};

class AstMemOrTableInit : public AstExpr {
  bool isMem_;
  uint32_t segIndex_;
  AstRef target_;
  AstExpr* dst_;
  AstExpr* src_;
  AstExpr* len_;

 public:
  static const AstExprKind Kind = AstExprKind::MemOrTableInit;
  explicit AstMemOrTableInit(bool isMem, uint32_t segIndex, AstRef target,
                             AstExpr* dst, AstExpr* src, AstExpr* len)
      : AstExpr(Kind, ExprType::Void),
        isMem_(isMem),
        segIndex_(segIndex),
        target_(target),
        dst_(dst),
        src_(src),
        len_(len) {}

  bool isMem() const { return isMem_; }
  uint32_t segIndex() const { return segIndex_; }
  AstRef& target() { return target_; }
  AstRef& targetTable() {
    MOZ_ASSERT(!isMem());
    return target_;
  }
  AstRef& targetMemory() {
    MOZ_ASSERT(isMem());
    return target_;
  }
  AstExpr& dst() const { return *dst_; }
  AstExpr& src() const { return *src_; }
  AstExpr& len() const { return *len_; }
};
#endif

#ifdef ENABLE_WASM_REFTYPES
class AstTableGet : public AstExpr {
  AstRef targetTable_;
  AstExpr* index_;

 public:
  static const AstExprKind Kind = AstExprKind::TableGet;
  explicit AstTableGet(AstRef targetTable, AstExpr* index)
      : AstExpr(Kind, ExprType::AnyRef),
        targetTable_(targetTable),
        index_(index) {}

  AstRef& targetTable() { return targetTable_; }
  AstExpr& index() const { return *index_; }
};

class AstTableGrow : public AstExpr {
  AstRef targetTable_;
  AstExpr* delta_;
  AstExpr* initValue_;

 public:
  static const AstExprKind Kind = AstExprKind::TableGrow;
  AstTableGrow(AstRef targetTable, AstExpr* delta, AstExpr* initValue)
      : AstExpr(Kind, ExprType::I32),
        targetTable_(targetTable),
        delta_(delta),
        initValue_(initValue) {}

  AstRef& targetTable() { return targetTable_; }
  AstExpr& delta() const { return *delta_; }
  AstExpr& initValue() const { return *initValue_; }
};

class AstTableSet : public AstExpr {
  AstRef targetTable_;
  AstExpr* index_;
  AstExpr* value_;

 public:
  static const AstExprKind Kind = AstExprKind::TableSet;
  AstTableSet(AstRef targetTable, AstExpr* index, AstExpr* value)
      : AstExpr(Kind, ExprType::Void),
        targetTable_(targetTable),
        index_(index),
        value_(value) {}

  AstRef& targetTable() { return targetTable_; }
  AstExpr& index() const { return *index_; }
  AstExpr& value() const { return *value_; }
};

class AstTableSize : public AstExpr {
  AstRef targetTable_;

 public:
  static const AstExprKind Kind = AstExprKind::TableSize;
  explicit AstTableSize(AstRef targetTable)
      : AstExpr(Kind, ExprType::I32), targetTable_(targetTable) {}

  AstRef& targetTable() { return targetTable_; }
};
#endif  // ENABLE_WASM_REFTYPES

#ifdef ENABLE_WASM_GC
class AstStructNew : public AstExpr {
  AstRef structType_;
  AstExprVector fieldValues_;

 public:
  static const AstExprKind Kind = AstExprKind::StructNew;
  AstStructNew(AstRef structType, AstExprType type, AstExprVector&& fieldVals)
      : AstExpr(Kind, type),
        structType_(structType),
        fieldValues_(std::move(fieldVals)) {}
  AstRef& structType() { return structType_; }
  const AstExprVector& fieldValues() const { return fieldValues_; }
};

class AstStructGet : public AstExpr {
  AstRef structType_;
  AstRef fieldName_;
  AstExpr* ptr_;

 public:
  static const AstExprKind Kind = AstExprKind::StructGet;
  AstStructGet(AstRef structType, AstRef fieldName, AstExprType type,
               AstExpr* ptr)
      : AstExpr(Kind, type),
        structType_(structType),
        fieldName_(fieldName),
        ptr_(ptr) {}
  AstRef& structType() { return structType_; }
  AstRef& fieldName() { return fieldName_; }
  AstExpr& ptr() const { return *ptr_; }
};

class AstStructSet : public AstExpr {
  AstRef structType_;
  AstRef fieldName_;
  AstExpr* ptr_;
  AstExpr* value_;

 public:
  static const AstExprKind Kind = AstExprKind::StructSet;
  AstStructSet(AstRef structType, AstRef fieldName, AstExpr* ptr,
               AstExpr* value)
      : AstExpr(Kind, ExprType::Void),
        structType_(structType),
        fieldName_(fieldName),
        ptr_(ptr),
        value_(value) {}
  AstRef& structType() { return structType_; }
  AstRef& fieldName() { return fieldName_; }
  AstExpr& ptr() const { return *ptr_; }
  AstExpr& value() const { return *value_; }
};

class AstStructNarrow : public AstExpr {
  AstValType inputStruct_;
  AstValType outputStruct_;
  AstExpr* ptr_;

 public:
  static const AstExprKind Kind = AstExprKind::StructNarrow;
  AstStructNarrow(AstValType inputStruct, AstValType outputStruct, AstExpr* ptr)
      : AstExpr(Kind, AstExprType(outputStruct)),
        inputStruct_(inputStruct),
        outputStruct_(outputStruct),
        ptr_(ptr) {}
  AstValType& inputStruct() { return inputStruct_; }
  AstValType& outputStruct() { return outputStruct_; }
  AstExpr& ptr() const { return *ptr_; }
};
#endif

class AstMemorySize final : public AstExpr {
 public:
  static const AstExprKind Kind = AstExprKind::MemorySize;
  explicit AstMemorySize() : AstExpr(Kind, ExprType::I32) {}
};

class AstMemoryGrow final : public AstExpr {
  AstExpr* operand_;

 public:
  static const AstExprKind Kind = AstExprKind::MemoryGrow;
  explicit AstMemoryGrow(AstExpr* operand)
      : AstExpr(Kind, ExprType::I32), operand_(operand) {}

  AstExpr* operand() const { return operand_; }
};

class AstBranchTable : public AstExpr {
  AstExpr& index_;
  AstRef default_;
  AstRefVector table_;
  AstExpr* value_;

 public:
  static const AstExprKind Kind = AstExprKind::BranchTable;
  explicit AstBranchTable(AstExpr& index, AstRef def, AstRefVector&& table,
                          AstExpr* maybeValue)
      : AstExpr(Kind, ExprType::Void),
        index_(index),
        default_(def),
        table_(std::move(table)),
        value_(maybeValue) {}
  AstExpr& index() const { return index_; }
  AstRef& def() { return default_; }
  AstRefVector& table() { return table_; }
  AstExpr* maybeValue() { return value_; }
};

class AstFunc : public AstNode {
  AstName name_;
  AstRef funcType_;
  AstValTypeVector vars_;
  AstNameVector localNames_;
  AstExprVector body_;

 public:
  AstFunc(AstName name, AstRef ft, AstValTypeVector&& vars,
          AstNameVector&& locals, AstExprVector&& body)
      : name_(name),
        funcType_(ft),
        vars_(std::move(vars)),
        localNames_(std::move(locals)),
        body_(std::move(body)) {}
  AstRef& funcType() { return funcType_; }
  const AstValTypeVector& vars() const { return vars_; }
  AstValTypeVector& vars() { return vars_; }
  const AstNameVector& locals() const { return localNames_; }
  const AstExprVector& body() const { return body_; }
  AstName name() const { return name_; }
};

class AstGlobal : public AstNode {
  AstName name_;
  bool isMutable_;
  AstValType type_;
  Maybe<AstExpr*> init_;

 public:
  AstGlobal() : isMutable_(false), type_(ValType()) {}

  explicit AstGlobal(AstName name, AstValType type, bool isMutable,
                     const Maybe<AstExpr*>& init = Maybe<AstExpr*>())
      : name_(name), isMutable_(isMutable), type_(type), init_(init) {}

  AstName name() const { return name_; }
  bool isMutable() const { return isMutable_; }
  ValType type() const { return type_.type(); }
  AstValType& type() { return type_; }

  bool hasInit() const { return !!init_; }
  AstExpr& init() const {
    MOZ_ASSERT(hasInit());
    return **init_;
  }
};

typedef AstVector<AstGlobal*> AstGlobalVector;

class AstImport : public AstNode {
  AstName name_;
  AstName module_;
  AstName field_;
  DefinitionKind kind_;

  AstRef funcType_;
  Limits limits_;
  TableKind tableKind_;
  AstGlobal global_;

 public:
  AstImport(AstName name, AstName module, AstName field, AstRef funcType)
      : name_(name),
        module_(module),
        field_(field),
        kind_(DefinitionKind::Function),
        funcType_(funcType) {}
  AstImport(AstName name, AstName module, AstName field, DefinitionKind kind,
            const Limits& limits)
      : name_(name),
        module_(module),
        field_(field),
        kind_(kind),
        limits_(limits) {
    MOZ_ASSERT(kind != DefinitionKind::Table, "A table must have a kind");
  }
  AstImport(AstName name, AstName module, AstName field, const Limits& limits,
            TableKind tableKind)
      : name_(name),
        module_(module),
        field_(field),
        kind_(DefinitionKind::Table),
        limits_(limits),
        tableKind_(tableKind) {}
  AstImport(AstName name, AstName module, AstName field,
            const AstGlobal& global)
      : name_(name),
        module_(module),
        field_(field),
        kind_(DefinitionKind::Global),
        global_(global) {}

  AstName name() const { return name_; }
  AstName module() const { return module_; }
  AstName field() const { return field_; }

  DefinitionKind kind() const { return kind_; }
  TableKind tableKind() const {
    MOZ_ASSERT(kind_ == DefinitionKind::Table);
    return tableKind_;
  }
  AstRef& funcType() {
    MOZ_ASSERT(kind_ == DefinitionKind::Function);
    return funcType_;
  }
  Limits limits() const {
    MOZ_ASSERT(kind_ == DefinitionKind::Memory ||
               kind_ == DefinitionKind::Table);
    return limits_;
  }
  const AstGlobal& global() const {
    MOZ_ASSERT(kind_ == DefinitionKind::Global);
    return global_;
  }
  AstGlobal& global() {
    MOZ_ASSERT(kind_ == DefinitionKind::Global);
    return global_;
  }
};

class AstExport : public AstNode {
  AstName name_;
  DefinitionKind kind_;
  AstRef ref_;

 public:
  AstExport(AstName name, DefinitionKind kind, AstRef ref)
      : name_(name), kind_(kind), ref_(ref) {}
  explicit AstExport(AstName name, DefinitionKind kind)
      : name_(name), kind_(kind) {}
  AstName name() const { return name_; }
  DefinitionKind kind() const { return kind_; }
  AstRef& ref() { return ref_; }
};

class AstDataSegment : public AstNode {
  AstExpr* offsetIfActive_;
  AstNameVector fragments_;

 public:
  AstDataSegment(AstExpr* offsetIfActive, AstNameVector&& fragments)
      : offsetIfActive_(offsetIfActive), fragments_(std::move(fragments)) {}

  AstExpr* offsetIfActive() const { return offsetIfActive_; }
  const AstNameVector& fragments() const { return fragments_; }
};

typedef AstVector<AstDataSegment*> AstDataSegmentVector;

struct AstNullValue {};
typedef Variant<AstRef, AstNullValue> AstElem;
typedef AstVector<AstElem> AstElemVector;

class AstElemSegment : public AstNode {
  AstRef targetTable_;
  AstExpr* offsetIfActive_;
  AstElemVector elems_;

 public:
  AstElemSegment(AstRef targetTable, AstExpr* offsetIfActive,
                 AstElemVector&& elems)
      : targetTable_(targetTable),
        offsetIfActive_(offsetIfActive),
        elems_(std::move(elems)) {}

  AstRef targetTable() const { return targetTable_; }
  AstRef& targetTableRef() { return targetTable_; }
  bool isPassive() const { return offsetIfActive_ == nullptr; }
  AstExpr* offsetIfActive() const { return offsetIfActive_; }
  AstElemVector& elems() { return elems_; }
  const AstElemVector& elems() const { return elems_; }
};

typedef AstVector<AstElemSegment*> AstElemSegmentVector;

class AstStartFunc : public AstNode {
  AstRef func_;

 public:
  explicit AstStartFunc(AstRef func) : func_(func) {}

  AstRef& func() { return func_; }
};

struct AstMemory {
  AstName name;
  Limits limits;
  bool imported;

  AstMemory(const Limits& limits, bool imported, AstName name = AstName())
      : name(name), limits(limits), imported(imported) {}
};

struct AstTable {
  AstName name;
  Limits limits;
  TableKind tableKind;
  bool imported;

  AstTable(const Limits& limits, TableKind tableKind, bool imported,
           AstName name = AstName())
      : name(name), limits(limits), tableKind(tableKind), imported(imported) {}
};

class AstModule : public AstNode {
 public:
  typedef AstVector<AstFunc*> FuncVector;
  typedef AstVector<AstImport*> ImportVector;
  typedef AstVector<AstExport*> ExportVector;
  typedef AstVector<AstTypeDef*> TypeDefVector;
  typedef AstVector<AstName> NameVector;
  typedef AstVector<AstMemory> AstMemoryVector;
  typedef AstVector<AstTable> AstTableVector;

 private:
  typedef AstHashMap<AstFuncType*, uint32_t, AstFuncType> FuncTypeMap;

  LifoAlloc& lifo_;
  TypeDefVector types_;
  FuncTypeMap funcTypeMap_;
  ImportVector imports_;
  NameVector funcImportNames_;
  AstTableVector tables_;
  AstMemoryVector memories_;
#ifdef ENABLE_WASM_GC
  uint32_t gcFeatureOptIn_;
#endif
  Maybe<uint32_t> dataCount_;
  ExportVector exports_;
  Maybe<AstStartFunc> startFunc_;
  FuncVector funcs_;
  AstDataSegmentVector dataSegments_;
  AstElemSegmentVector elemSegments_;
  AstGlobalVector globals_;

  size_t numGlobalImports_;

 public:
  explicit AstModule(LifoAlloc& lifo)
      : lifo_(lifo),
        types_(lifo),
        funcTypeMap_(lifo),
        imports_(lifo),
        funcImportNames_(lifo),
        tables_(lifo),
        memories_(lifo),
#ifdef ENABLE_WASM_GC
        gcFeatureOptIn_(0),
#endif
        exports_(lifo),
        funcs_(lifo),
        dataSegments_(lifo),
        elemSegments_(lifo),
        globals_(lifo),
        numGlobalImports_(0) {
  }
  bool addMemory(AstName name, const Limits& memory) {
    return memories_.append(AstMemory(memory, false, name));
  }
  bool hasMemory() const { return !!memories_.length(); }
  const AstMemoryVector& memories() const { return memories_; }
#ifdef ENABLE_WASM_GC
  bool addGcFeatureOptIn(uint32_t version) {
    gcFeatureOptIn_ = version;
    return true;
  }
  uint32_t gcFeatureOptIn() const { return gcFeatureOptIn_; }
#endif
  bool initDataCount(uint32_t dataCount) {
    MOZ_ASSERT(dataCount_.isNothing());
    dataCount_.emplace(dataCount);
    return true;
  }
  Maybe<uint32_t> dataCount() const { return dataCount_; }
  bool addTable(AstName name, const Limits& table, TableKind tableKind) {
    return tables_.append(AstTable(table, tableKind, false, name));
  }
  bool hasTable() const { return !!tables_.length(); }
  const AstTableVector& tables() const { return tables_; }
  bool append(AstDataSegment* seg) { return dataSegments_.append(seg); }
  const AstDataSegmentVector& dataSegments() const { return dataSegments_; }
  bool append(AstElemSegment* seg) { return elemSegments_.append(seg); }
  const AstElemSegmentVector& elemSegments() const { return elemSegments_; }
  bool hasStartFunc() const { return !!startFunc_; }
  bool setStartFunc(AstStartFunc startFunc) {
    if (startFunc_) {
      return false;
    }
    startFunc_.emplace(startFunc);
    return true;
  }
  AstStartFunc& startFunc() { return *startFunc_; }
  bool declare(AstFuncType&& funcType, uint32_t* funcTypeIndex) {
    FuncTypeMap::AddPtr p = funcTypeMap_.lookupForAdd(funcType);
    if (p) {
      *funcTypeIndex = p->value();
      return true;
    }
    *funcTypeIndex = types_.length();
    auto* lifoFuncType =
        new (lifo_) AstFuncType(AstName(), std::move(funcType));
    return lifoFuncType && types_.append(lifoFuncType) &&
           funcTypeMap_.add(p, static_cast<AstFuncType*>(types_.back()),
                            *funcTypeIndex);
  }
  bool append(AstFuncType* funcType) {
    uint32_t funcTypeIndex = types_.length();
    if (!types_.append(funcType)) {
      return false;
    }
    FuncTypeMap::AddPtr p = funcTypeMap_.lookupForAdd(*funcType);
    return p || funcTypeMap_.add(p, funcType, funcTypeIndex);
  }
  const TypeDefVector& types() const { return types_; }
  bool append(AstFunc* func) { return funcs_.append(func); }
  const FuncVector& funcs() const { return funcs_; }
  bool append(AstStructType* str) { return types_.append(str); }
  bool append(AstTypeDef* td) {
    if (td->isFuncType()) {
      return append(&td->asFuncType());
    }
    if (td->isStructType()) {
      return append(&td->asStructType());
    }
    MOZ_CRASH("Bad type");
  }
  bool append(AstImport* imp) {
    switch (imp->kind()) {
      case DefinitionKind::Function:
        if (!funcImportNames_.append(imp->name())) {
          return false;
        }
        break;
      case DefinitionKind::Table:
        if (!tables_.append(AstTable(imp->limits(), imp->tableKind(), true))) {
          return false;
        }
        break;
      case DefinitionKind::Memory:
        if (!memories_.append(AstMemory(imp->limits(), true))) {
          return false;
        }
        break;
      case DefinitionKind::Global:
        numGlobalImports_++;
        break;
    }
    return imports_.append(imp);
  }
  const ImportVector& imports() const { return imports_; }
  const NameVector& funcImportNames() const { return funcImportNames_; }
  size_t numFuncImports() const { return funcImportNames_.length(); }
  bool append(AstExport* exp) { return exports_.append(exp); }
  const ExportVector& exports() const { return exports_; }
  bool append(AstGlobal* glob) { return globals_.append(glob); }
  const AstGlobalVector& globals() const { return globals_; }
  size_t numGlobalImports() const { return numGlobalImports_; }
};

class AstUnaryOperator final : public AstExpr {
  Op op_;
  AstExpr* operand_;

 public:
  static const AstExprKind Kind = AstExprKind::UnaryOperator;
  explicit AstUnaryOperator(Op op, AstExpr* operand)
      : AstExpr(Kind, ExprType::Limit), op_(op), operand_(operand) {}

  Op op() const { return op_; }
  AstExpr* operand() const { return operand_; }
};

class AstBinaryOperator final : public AstExpr {
  Op op_;
  AstExpr* lhs_;
  AstExpr* rhs_;

 public:
  static const AstExprKind Kind = AstExprKind::BinaryOperator;
  explicit AstBinaryOperator(Op op, AstExpr* lhs, AstExpr* rhs)
      : AstExpr(Kind, ExprType::Limit), op_(op), lhs_(lhs), rhs_(rhs) {}

  Op op() const { return op_; }
  AstExpr* lhs() const { return lhs_; }
  AstExpr* rhs() const { return rhs_; }
};

class AstTernaryOperator : public AstExpr {
  Op op_;
  AstExpr* op0_;
  AstExpr* op1_;
  AstExpr* op2_;

 public:
  static const AstExprKind Kind = AstExprKind::TernaryOperator;
  AstTernaryOperator(Op op, AstExpr* op0, AstExpr* op1, AstExpr* op2)
      : AstExpr(Kind, ExprType::Limit),
        op_(op),
        op0_(op0),
        op1_(op1),
        op2_(op2) {}

  Op op() const { return op_; }
  AstExpr* op0() const { return op0_; }
  AstExpr* op1() const { return op1_; }
  AstExpr* op2() const { return op2_; }
};

class AstComparisonOperator final : public AstExpr {
  Op op_;
  AstExpr* lhs_;
  AstExpr* rhs_;

 public:
  static const AstExprKind Kind = AstExprKind::ComparisonOperator;
  explicit AstComparisonOperator(Op op, AstExpr* lhs, AstExpr* rhs)
      : AstExpr(Kind, ExprType::Limit), op_(op), lhs_(lhs), rhs_(rhs) {}

  Op op() const { return op_; }
  AstExpr* lhs() const { return lhs_; }
  AstExpr* rhs() const { return rhs_; }
};

class AstConversionOperator final : public AstExpr {
  Op op_;
  AstExpr* operand_;

 public:
  static const AstExprKind Kind = AstExprKind::ConversionOperator;
  explicit AstConversionOperator(Op op, AstExpr* operand)
      : AstExpr(Kind, ExprType::Limit), op_(op), operand_(operand) {}

  Op op() const { return op_; }
  AstExpr* operand() const { return operand_; }
};

// Like AstConversionOperator, but for opcodes encoded with the Misc prefix.
class AstExtraConversionOperator final : public AstExpr {
  MiscOp op_;
  AstExpr* operand_;

 public:
  static const AstExprKind Kind = AstExprKind::ExtraConversionOperator;
  explicit AstExtraConversionOperator(MiscOp op, AstExpr* operand)
      : AstExpr(Kind, ExprType::Limit), op_(op), operand_(operand) {}

  MiscOp op() const { return op_; }
  AstExpr* operand() const { return operand_; }
};

class AstRefNull final : public AstExpr {
 public:
  static const AstExprKind Kind = AstExprKind::RefNull;
  AstRefNull() : AstExpr(Kind, ExprType::Limit) {}
};

// This is an artificial AST node which can fill operand slots in an AST
// constructed from parsing or decoding stack-machine code that doesn't have
// an inherent AST structure.
class AstPop final : public AstExpr {
 public:
  static const AstExprKind Kind = AstExprKind::Pop;
  AstPop() : AstExpr(Kind, ExprType::Void) {}
};

// This is an artificial AST node which can be used to represent some forms
// of stack-machine code in an AST form. It is similar to Block, but returns the
// value of its first operand, rather than the last.
class AstFirst : public AstExpr {
  AstExprVector exprs_;

 public:
  static const AstExprKind Kind = AstExprKind::First;
  explicit AstFirst(AstExprVector&& exprs)
      : AstExpr(Kind, ExprType::Limit), exprs_(std::move(exprs)) {}

  AstExprVector& exprs() { return exprs_; }
  const AstExprVector& exprs() const { return exprs_; }
};

}  // namespace wasm
}  // namespace js

#endif  // namespace wasmast_h