aboutsummaryrefslogtreecommitdiffstats
path: root/src/servo/dom/bindings/codegen/CodegenRust.py
blob: 44d01e4ed9a0f53ad7855217599091b897be7006 (plain) (blame)
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

# Common codegen classes.

import os
import string
import operator

from WebIDL import *
from Configuration import NoSuchDescriptorError

AUTOGENERATED_WARNING_COMMENT = \
    "/* THIS FILE IS AUTOGENERATED - DO NOT EDIT */\n\n"
ADDPROPERTY_HOOK_NAME = '_addProperty'
FINALIZE_HOOK_NAME = '_finalize'
TRACE_HOOK_NAME = '_trace'
CONSTRUCT_HOOK_NAME = '_constructor'
HASINSTANCE_HOOK_NAME = '_hasInstance'

def replaceFileIfChanged(filename, newContents):
    """
    Read a copy of the old file, so that we don't touch it if it hasn't changed.
    Returns True if the file was updated, false otherwise.
    """
    oldFileContents = ""
    try:
        oldFile = open(filename, 'rb')
        oldFileContents = ''.join(oldFile.readlines())
        oldFile.close()
    except:
        pass

    if newContents == oldFileContents:
        return False

    f = open(filename, 'wb')
    f.write(newContents)
    f.close()

def toStringBool(arg):
    return str(not not arg).lower()

def toBindingNamespace(arg):
    return re.sub("((_workers)?$)", "Binding\\1", arg);

def stripTrailingWhitespace(text):
    tail = '\n' if text.endswith('\n') else ''
    lines = text.splitlines()
    for i in range(len(lines)):
        lines[i] = lines[i].rstrip()
    return '\n'.join(lines) + tail

def MakeNativeName(name):
    return name[0].upper() + name[1:]

builtinNames = {
    IDLType.Tags.bool: 'bool',
    IDLType.Tags.int8: 'i8',
    IDLType.Tags.int16: 'i16',
    IDLType.Tags.int32: 'i32',
    IDLType.Tags.int64: 'i64',
    IDLType.Tags.uint8: 'u8',
    IDLType.Tags.uint16: 'u16',
    IDLType.Tags.uint32: 'u32',
    IDLType.Tags.uint64: 'u64',
    IDLType.Tags.float: 'f32',
    IDLType.Tags.double: 'f64'
}

class CastableObjectUnwrapper():
    """
    A class for unwrapping an object named by the "source" argument
    based on the passed-in descriptor and storing it in a variable
    called by the name in the "target" argument.

    codeOnFailure is the code to run if unwrapping fails.
    """
    def __init__(self, descriptor, source, target, codeOnFailure):
        assert descriptor.castable

        self.substitution = { "type" : descriptor.nativeType,
                              "protoID" : "prototypes::id::" + descriptor.name,
                              "source" : source,
                              "target" : target,
                              "codeOnFailure" : CGIndenter(CGGeneric(codeOnFailure), 4).define() }
        if descriptor.hasXPConnectImpls:
            # We don't use xpc_qsUnwrapThis because it will always throw on
            # unwrap failure, whereas we want to control whether we throw or
            # not.
            self.substitution["codeOnFailure"] = CGIndenter(CGGeneric(string.Template(
                "${type} *objPtr;\n"
                "xpc_qsSelfRef objRef;\n"
                "JS::Value val = JS::ObjectValue(*${source});\n"
                "nsresult rv = xpc_qsUnwrapArg<${type}>(cx, val, &objPtr, &objRef.ptr, &val);\n"
                "if (NS_FAILED(rv)) {\n"
                "${codeOnFailure}\n"
                "}\n"
                "// We should be castable!\n"
                "MOZ_ASSERT(!objRef.ptr);\n"
                "// We should have an object, too!\n"
                "MOZ_ASSERT(objPtr);\n"
                "${target} = objPtr;").substitute(self.substitution)), 4).define()

    def __str__(self):
        return string.Template(
"""${target} = unwrap<${type}>(${source});
""").substitute(self.substitution)
#"""{
#  nsresult rv = UnwrapObject<${protoID}, ${type}>(cx, ${source}, ${target});
#  if (NS_FAILED(rv)) {
#${codeOnFailure}
#  }
#}""").substitute(self.substitution)

class FakeCastableDescriptor():
    def __init__(self, descriptor):
        self.castable = True
        self.workers = descriptor.workers
        self.nativeType = descriptor.nativeType
        self.name = descriptor.name
        self.hasXPConnectImpls = descriptor.hasXPConnectImpls

def getWrapTemplateForType(type, descriptorProvider, result, successCode,
                           isCreator):
    """
    Reflect a C++ value stored in "result", of IDL type "type" into JS.  The
    "successCode" is the code to run once we have successfully done the
    conversion.  The resulting string should be used with string.Template, it
    needs the following keys when substituting: jsvalPtr/jsvalRef/obj.

    Returns (templateString, infallibility of conversion template)
    """
    haveSuccessCode = successCode is not None
    if not haveSuccessCode:
        successCode = "return true;"

    def setValue(value, callWrapValue=False):
        """
        Returns the code to set the jsval to value. If "callWrapValue" is true
        JS_WrapValue will be called on the jsval.
        """
        if not callWrapValue:
            tail = successCode
        elif haveSuccessCode:
            tail = ("if (!JS_WrapValue(cx, ${jsvalPtr})) {\n" +
                    "  return false;\n" +
                    "}\n" +
                    successCode)
        else:
            tail = "return JS_WrapValue(cx, ${jsvalPtr});"
        return ("${jsvalRef} = %s;\n" +
                tail) % (value)

    def wrapAndSetPtr(wrapCall, failureCode=None):
        """
        Returns the code to set the jsval by calling "wrapCall". "failureCode"
        is the code to run if calling "wrapCall" fails 
        """
        if failureCode is None:
            if not haveSuccessCode:
                return "return " + wrapCall + ";"
            failureCode = "return false;"
        str = ("if (!%s) {\n" +
               CGIndenter(CGGeneric(failureCode)).define() + "\n" +
               "}\n" +
               successCode) % (wrapCall)
        return str
    
    if type is None or type.isVoid():
        return (setValue("JSVAL_VOID"), True)

    if type.isArray():
        raise TypeError("Can't handle array return values yet")

    if type.isSequence():
        if type.nullable():
            # Nullable sequences are Nullable< nsTArray<T> >
            (recTemplate, recInfall) = getWrapTemplateForType(type.inner, descriptorProvider,
                                                              "%s.Value()" % result, successCode,
                                                              isCreator)
            return ("""
if (%s.IsNull()) {
%s
}
%s""" % (result, CGIndenter(CGGeneric(setValue("JSVAL_NULL"))).define(), recTemplate), recInfall)

        # Now do non-nullable sequences.  We use setting the element
        # in the array as our succcess code because when we succeed in
        # wrapping that's what we should do.
        innerTemplate = wrapForType(
            type.inner, descriptorProvider,
            {
                'result' :  "%s[i]" % result,
                'successCode': ("if (!JS_DefineElement(cx, returnArray, i, tmp,\n"
                                "                      NULL, NULL, JSPROP_ENUMERATE)) {\n"
                                "  return false;\n"
                                "}"),
                'jsvalRef': "tmp",
                'jsvalPtr': "&tmp",
                'isCreator': isCreator
                }
            )
        innerTemplate = CGIndenter(CGGeneric(innerTemplate)).define()
        return (("""
uint32_t length = %s.Length();
JSObject *returnArray = JS_NewArrayObject(cx, length, NULL);
if (!returnArray) {
  return false;
}
jsval tmp;
for (uint32_t i = 0; i < length; ++i) {
%s
}\n""" % (result, innerTemplate)) + setValue("JS::ObjectValue(*returnArray)"), False)

    if type.isGeckoInterface():
        descriptor = descriptorProvider.getDescriptor(type.unroll().inner.identifier.name)
        if type.nullable():
            wrappingCode = ("if (!%s) {\n" % (result) +
                            CGIndenter(CGGeneric(setValue("JSVAL_NULL"))).define() + "\n" +
                            "}\n")
        else:
            wrappingCode = ""
        if (not descriptor.interface.isExternal() and
            not descriptor.interface.isCallback()):
            if descriptor.wrapperCache:
                wrapMethod = "WrapNewBindingObject"
            else:
                if not isCreator:
                    raise MethodNotCreatorError(descriptor.interface.identifier.name)
                wrapMethod = "WrapNewBindingNonWrapperCachedObject"
            wrap = "%s(cx, ${obj}, %s, ${jsvalPtr})" % (wrapMethod, result)
            # We don't support prefable stuff in workers.
            assert(not descriptor.prefable or not descriptor.workers)
            if not descriptor.prefable:
                # Non-prefable bindings can only fail to wrap as a new-binding object
                # if they already threw an exception.  Same thing for
                # non-prefable bindings.
                failed = ("MOZ_ASSERT(JS_IsExceptionPending(cx));\n" +
                          "return false;")
            else:
                if descriptor.notflattened:
                    raise TypeError("%s is prefable but not flattened; "
                                    "fallback won't work correctly" %
                                    descriptor.interface.identifier.name)
                # Try old-style wrapping for bindings which might be preffed off.
                failed = wrapAndSetPtr("HandleNewBindingWrappingFailure(cx, ${obj}, %s, ${jsvalPtr})" % result)
            wrappingCode += wrapAndSetPtr(wrap, failed)
        else:
            if descriptor.notflattened:
                getIID = "&NS_GET_IID(%s), " % descriptor.nativeType
            else:
                getIID = ""
            wrap = "WrapObject(cx, ${obj}, %s, %s${jsvalPtr})" % (result, getIID)
            wrappingCode += wrapAndSetPtr(wrap)
        return (wrappingCode, False)

    if type.isString():
        if type.nullable():
            return (wrapAndSetPtr("xpc::StringToJsval(cx, %s, ${jsvalPtr})" % result), False)
        else:
            return (wrapAndSetPtr("xpc::NonVoidStringToJsval(cx, %s, ${jsvalPtr})" % result), False)

    if type.isEnum():
        if type.nullable():
            raise TypeError("We don't support nullable enumerated return types "
                            "yet")
        return ("""MOZ_ASSERT(uint32_t(%(result)s) < ArrayLength(%(strings)s));
JSString* %(resultStr)s = JS_NewStringCopyN(cx, %(strings)s[uint32_t(%(result)s)].value, %(strings)s[uint32_t(%(result)s)].length);
if (!%(resultStr)s) {
  return false;
}
""" % { "result" : result,
        "resultStr" : result + "_str",
        "strings" : type.inner.identifier.name + "Values::strings" } +
        setValue("JS::StringValue(%s_str)" % result), False)

    if type.isCallback():
        assert not type.isInterface()
        # XXXbz we're going to assume that callback types are always
        # nullable and always have [TreatNonCallableAsNull] for now.
        # See comments in WrapNewBindingObject explaining why we need
        # to wrap here.
        # NB: setValue(..., True) calls JS_WrapValue(), so is fallible
        return (setValue("JS::ObjectOrNullValue(%s)" % result, True), False)

    if type.tag() == IDLType.Tags.any:
        # See comments in WrapNewBindingObject explaining why we need
        # to wrap here.
        # NB: setValue(..., True) calls JS_WrapValue(), so is fallible
        return (setValue(result, True), False)

    if type.isObject() or type.isSpiderMonkeyInterface():
        # See comments in WrapNewBindingObject explaining why we need
        # to wrap here.
        if type.nullable():
            toValue = "JS::ObjectOrNullValue(%s)"
        else:
            toValue = "JS::ObjectValue(*%s)"
        # NB: setValue(..., True) calls JS_WrapValue(), so is fallible
        return (setValue(toValue % result, True), False)

    if not type.isPrimitive():
        raise TypeError("Need to learn to wrap %s" % type)

    if type.nullable():
        (recTemplate, recInfal) = getWrapTemplateForType(type.inner, descriptorProvider,
                                                         "%s.Value()" % result, successCode,
                                                         isCreator)
        return ("if (%s.IsNull()) {\n" % result +
                CGIndenter(CGGeneric(setValue("JSVAL_NULL"))).define() + "\n" +
                "}\n" + recTemplate, recInfal)
    
    tag = type.tag()
    
    if tag in [IDLType.Tags.int8, IDLType.Tags.uint8, IDLType.Tags.int16,
               IDLType.Tags.uint16, IDLType.Tags.int32]:
        return (setValue("INT_TO_JSVAL(int32_t(%s))" % result), True)

    elif tag in [IDLType.Tags.int64, IDLType.Tags.uint64, IDLType.Tags.float,
                 IDLType.Tags.double]:
        # XXXbz will cast to double do the "even significand" thing that webidl
        # calls for for 64-bit ints?  Do we care?
        return (setValue("JS_NumberValue(%s as f64)" % result), True)

    elif tag == IDLType.Tags.uint32:
        return (setValue("UINT_TO_JSVAL(%s)" % result), True)

    elif tag == IDLType.Tags.bool:
        return (setValue("BOOLEAN_TO_JSVAL(%s)" % result), True)

    else:
        raise TypeError("Need to learn to wrap primitive: %s" % type)

def wrapForType(type, descriptorProvider, templateValues):
    """
    Reflect a C++ value of IDL type "type" into JS.  TemplateValues is a dict
    that should contain:

      * 'jsvalRef': a C++ reference to the jsval in which to store the result of
                    the conversion
      * 'jsvalPtr': a C++ pointer to the jsval in which to store the result of
                    the conversion
      * 'obj' (optional): the name of the variable that contains the JSObject to
                          use as a scope when wrapping, if not supplied 'obj'
                          will be used as the name
      * 'result' (optional): the name of the variable in which the C++ value is
                             stored, if not supplied 'result' will be used as
                             the name
      * 'successCode' (optional): the code to run once we have successfully done
                                  the conversion, if not supplied 'return true;'
                                  will be used as the code
      * 'isCreator' (optional): If true, we're wrapping for the return value of
                                a [Creator] method.  Assumed false if not set.
    """
    wrap = getWrapTemplateForType(type, descriptorProvider,
                                  templateValues.get('result', 'result'),
                                  templateValues.get('successCode', None),
                                  templateValues.get('isCreator', False))[0]

    defaultValues = {'obj': 'obj'}
    return string.Template(wrap).substitute(defaultValues, **templateValues)

def typeNeedsCx(type, retVal=False):
    if type is None:
        return False
    if type.nullable():
        type = type.inner
    if type.isSequence() or type.isArray():
        type = type.inner
    if type.isUnion():
        return any(typeNeedsCx(t) for t in type.unroll().flatMemberTypes)
    if retVal and type.isSpiderMonkeyInterface():
        return True
    return type.isCallback() or type.isAny() or type.isObject()

def memberIsCreator(member):
    return member.getExtendedAttribute("Creator") is not None

# Returns a tuple consisting of a CGThing containing the type of the return
# value, or None if there is no need for a return value, and a boolean signaling
# whether the return value is passed in an out parameter.
def getRetvalDeclarationForType(returnType, descriptorProvider,
                                resultAlreadyAddRefed):
    if returnType is None or returnType.isVoid():
        # Nothing to declare
        return None, False
    if returnType.isPrimitive() and returnType.tag() in builtinNames:
        result = CGGeneric(builtinNames[returnType.tag()])
        if returnType.nullable():
            result = CGWrapper(result, pre="Nullable<", post=">")
        return result, False
    if returnType.isString():
        return CGGeneric("nsString"), True
    if returnType.isEnum():
        if returnType.nullable():
            raise TypeError("We don't support nullable enum return values")
        return CGGeneric(returnType.inner.identifier.name), False
    if returnType.isGeckoInterface():
        result = CGGeneric(descriptorProvider.getDescriptor(
            returnType.unroll().inner.identifier.name).nativeType)
        if resultAlreadyAddRefed:
            result = CGWrapper(result, pre="nsRefPtr<", post=">")
        else:
            result = CGWrapper(result, post="*")
        return result, False
    if returnType.isCallback():
        # XXXbz we're going to assume that callback types are always
        # nullable for now.
        return CGGeneric("*JSObject"), False
    if returnType.isAny():
        return CGGeneric("jsval"), False
    if returnType.isObject() or returnType.isSpiderMonkeyInterface():
        return CGGeneric("*JSObject"), False
    if returnType.isSequence():
        nullable = returnType.nullable()
        if nullable:
            returnType = returnType.inner
        # If our result is already addrefed, use the right type in the
        # sequence argument here.
        (result, _) = getRetvalDeclarationForType(returnType.inner,
                                                  descriptorProvider,
                                                  resultAlreadyAddRefed)
        result = CGWrapper(result, pre="nsTArray< ", post=" >")
        if nullable:
            result = CGWrapper(result, pre="Nullable< ", post=" >")
        return result, True
    raise TypeError("Don't know how to declare return value for %s" %
                    returnType)

class PropertyArrays():
    def __init__(self, descriptor):
        #self.staticMethods = MethodDefiner(descriptor, "StaticMethods", True)
        #self.methods = MethodDefiner(descriptor, "Methods", False)
        #self.attrs = AttrDefiner(descriptor, "Attributes")
        #self.consts = ConstDefiner(descriptor, "Constants")
        pass

    @staticmethod
    def arrayNames():
        return [ "staticMethods", "methods", "attrs", "consts" ]

    @staticmethod
    def xrayRelevantArrayNames():
        return [ "methods", "attrs", "consts" ]

    def hasChromeOnly(self):
        return reduce(lambda b, a: b or getattr(self, a).hasChromeOnly(),
                      self.arrayNames(), False)
    def variableNames(self, chrome):
        names = {}
        for array in self.arrayNames():
            names[array] = getattr(self, array).variableName(chrome)
        return names
    def __str__(self):
        define = ""
        for array in self.arrayNames():
            define += str(getattr(self, array))
        return define

class CGThing():
    """
    Abstract base class for things that spit out code.
    """
    def __init__(self):
        pass # Nothing for now
    def declare(self):
        """Produce code for a header file."""
        assert(False)  # Override me!
    def define(self):
        """Produce code for a cpp file."""
        assert(False) # Override me!

# We'll want to insert the indent at the beginnings of lines, but we
# don't want to indent empty lines.  So only indent lines that have a
# non-newline character on them.
lineStartDetector = re.compile("^(?=[^\n#])", re.MULTILINE)
class CGIndenter(CGThing):
    """
    A class that takes another CGThing and generates code that indents that
    CGThing by some number of spaces.  The default indent is two spaces.
    """
    def __init__(self, child, indentLevel=2, declareOnly=False):
        CGThing.__init__(self)
        self.child = child
        self.indent = " " * indentLevel
        self.declareOnly = declareOnly
    def declare(self):
        decl = self.child.declare()
        if decl is not "":
            return re.sub(lineStartDetector, self.indent, decl)
        else:
            return ""
    def define(self):
        defn = self.child.define()
        if defn is not "" and not self.declareOnly:
            return re.sub(lineStartDetector, self.indent, defn)
        else:
            return defn

class CGWrapper(CGThing):
    """
    Generic CGThing that wraps other CGThings with pre and post text.
    """
    def __init__(self, child, pre="", post="", declarePre=None,
                 declarePost=None, definePre=None, definePost=None,
                 declareOnly=False, defineOnly=False, reindent=False):
        CGThing.__init__(self)
        self.child = child
        self.declarePre = declarePre or pre
        self.declarePost = declarePost or post
        self.definePre = definePre or pre
        self.definePost = definePost or post
        self.declareOnly = declareOnly
        self.defineOnly = defineOnly
        self.reindent = reindent
    def declare(self):
        if self.defineOnly:
            return ''
        decl = self.child.declare()
        if self.reindent:
            # We don't use lineStartDetector because we don't want to
            # insert whitespace at the beginning of our _first_ line.
            decl = stripTrailingWhitespace(
                decl.replace("\n", "\n" + (" " * len(self.declarePre))))
        return self.declarePre + decl + self.declarePost
    def define(self):
        if self.declareOnly:
            return ''
        defn = self.child.define()
        if self.reindent:
            # We don't use lineStartDetector because we don't want to
            # insert whitespace at the beginning of our _first_ line.
            defn = stripTrailingWhitespace(
                defn.replace("\n", "\n" + (" " * len(self.definePre))))
        return self.definePre + defn + self.definePost

class CGImports(CGWrapper):
    """
    Generates the appropriate import/use statements.
    """
    def __init__(self, descriptors, dictionaries, declareImports, defineImports, child):
        """
        Builds a set of imports to cover |descriptors|.

        Also includes the files in |declareIncludes| in the header
        file and the files in |defineIncludes| in the .cpp.
        """

        # TODO imports to cover descriptors, etc.

        def _useString(imports):
            return ''.join(['use %s;\n' % i for i in imports]) + '\n'
        CGWrapper.__init__(self, child,
                           definePre=_useString(sorted(defineImports)))

class CGNamespace(CGWrapper):
    def __init__(self, namespace, child, declareOnly=False):
        pre = "mod %s {\n" % namespace
        post = "} // mod %s\n" % namespace
        CGWrapper.__init__(self, child, pre=pre, post=post,
                           declareOnly=declareOnly)
    @staticmethod
    def build(namespaces, child, declareOnly=False):
        """
        Static helper method to build multiple wrapped namespaces.
        """
        if not namespaces:
            return CGWrapper(child, declareOnly=declareOnly)
        inner = CGNamespace.build(namespaces[1:], child, declareOnly=declareOnly)
        return CGNamespace(namespaces[0], inner, declareOnly=declareOnly)

class CGList(CGThing):
    """
    Generate code for a list of GCThings.  Just concatenates them together, with
    an optional joiner string.  "\n" is a common joiner.
    """
    def __init__(self, children, joiner=""):
        CGThing.__init__(self)
        self.children = children
        self.joiner = joiner
    def append(self, child):
        self.children.append(child)
    def prepend(self, child):
        self.children.insert(0, child)
    def join(self, generator):
        return self.joiner.join(filter(lambda s: len(s) > 0, (child for child in generator)))
    def declare(self):
        return self.join(child.declare() for child in self.children if child is not None)
    def define(self):
        return self.join(child.define() for child in self.children if child is not None)

class CGGeneric(CGThing):
    """
    A class that spits out a fixed string into the codegen.  Can spit out a
    separate string for the declaration too.
    """
    def __init__(self, define="", declare=""):
        self.declareText = declare
        self.defineText = define
    def declare(self):
        return self.declareText
    def define(self):
        return self.defineText

class Argument():
    """
    A class for outputting the type and name of an argument
    """
    def __init__(self, argType, name):
        self.argType = argType
        self.name = name
    def __str__(self):
        return self.name + ': ' + self.argType

class CGAbstractMethod(CGThing):
    """
    An abstract class for generating code for a method.  Subclasses
    should override definition_body to create the actual code.

    descriptor is the descriptor for the interface the method is associated with

    name is the name of the method as a string

    returnType is the IDLType of the return value

    args is a list of Argument objects

    inline should be True to generate an inline method, whose body is
    part of the declaration.

    alwaysInline should be True to generate an inline method annotated with
    MOZ_ALWAYS_INLINE.

    static should be True to generate a static method, which only has
    a definition.

    If templateArgs is not None it should be a list of strings containing
    template arguments, and the function will be templatized using those
    arguments.
    """
    def __init__(self, descriptor, name, returnType, args, inline=False, alwaysInline=False, static=False, extern=False, templateArgs=None):
        CGThing.__init__(self)
        self.descriptor = descriptor
        self.name = name
        self.returnType = returnType
        self.args = args
        self.inline = inline
        self.alwaysInline = alwaysInline
        self.static = static
        self.extern = extern
        self.templateArgs = templateArgs
    def _argstring(self):
        return ', '.join([str(a) for a in self.args])
    def _template(self):
        if self.templateArgs is None:
            return ''
        return '<%s>\n' % ', '.join(self.templateArgs)
    def _decorators(self):
        decorators = []
        if self.alwaysInline:
            decorators.append('#[inline(always)]')
        elif self.inline:
            #decorators.append('inline')
            pass
        if self.extern:
            decorators.append('extern')
        if self.static:
            #decorators.append('static')
            pass
        if not decorators:
            return ''
        #maybeNewline = " " if self.inline else "\n"
        maybeNewline = " "
        return ' '.join(decorators) + maybeNewline
    def _returnType(self):
        return (" -> %s" % self.returnType) if self.returnType != "void" else ""
    def declare(self):
        if self.inline:
            return self._define()
        return "%sfn %s%s(%s)%s;\n" % (self._decorators(), self.name, self._template(),
                                       self.name, self._argstring(), self._returnType())
    def _define(self):
        return self.definition_prologue() + "\n" + self.definition_body() + self.definition_epilogue()
    def define(self):
        return "" if self.inline else self._define()
    def definition_prologue(self):
        return "%sfn %s%s(%s)%s unsafe {" % (self._decorators(), self.name, self._template(), 
                                             self._argstring(), self._returnType())
    def definition_epilogue(self):
        return "\n}\n"
    def definition_body(self):
        assert(False) # Override me!

class CGAbstractExternMethod(CGAbstractMethod):
    """
    Abstract base class for codegen of implementation-only (no
    declaration) static methods.
    """
    def __init__(self, descriptor, name, returnType, args):
        CGAbstractMethod.__init__(self, descriptor, name, returnType, args,
                                  inline=False, extern=True)
    def declare(self):
        # We only have implementation
        return ""

class CGCallGenerator(CGThing):
    """
    A class to generate an actual call to a C++ object.  Assumes that the C++
    object is stored in a variable whose name is given by the |object| argument.

    errorReport should be a CGThing for an error report or None if no
    error reporting is needed.
    """
    def __init__(self, errorReport, arguments, argsPre, returnType,
                 extendedAttributes, descriptorProvider, nativeMethodName,
                 static, object="self", declareResult=True):
        CGThing.__init__(self)

        assert errorReport is None or isinstance(errorReport, CGThing)

        isFallible = errorReport is not None

        #resultAlreadyAddRefed = isResultAlreadyAddRefed(descriptorProvider,
        #                                                extendedAttributes)
        resultAlreadyAddRefed = True
        (result, resultOutParam) = getRetvalDeclarationForType(returnType,
                                                               descriptorProvider,
                                                               resultAlreadyAddRefed)

        args = CGList([CGGeneric(arg) for arg in argsPre], ", ")
        for (a, name) in arguments:
            # This is a workaround for a bug in Apple's clang.
            if a.type.isObject() and not a.type.nullable() and not a.optional:
                name = "(JSObject&)" + name
            args.append(CGGeneric(name))

        # Return values that go in outparams go here
        if resultOutParam:
            args.append(CGGeneric("result"))
        if isFallible:
            args.append(CGGeneric("rv"))

        needsCx = (typeNeedsCx(returnType, True) or
                   any(typeNeedsCx(a.type) for (a, _) in arguments) or
                   'implicitJSContext' in extendedAttributes)

        if not "cx" in argsPre and needsCx:
            args.prepend(CGGeneric("cx"))

        # Build up our actual call
        self.cgRoot = CGList([], "\n")

        call = CGGeneric(nativeMethodName)
        if static:
            call = CGWrapper(call, pre="%s::" % descriptorProvider.nativeType)
        else: 
            call = CGWrapper(call, pre="(*%s)." % object)
        call = CGList([call, CGWrapper(args, pre="(", post=");")])
        if result is not None:
            if declareResult:
                result = CGWrapper(result, pre="let result: ", post=";")
                self.cgRoot.prepend(result)
            if not resultOutParam:
                call = CGWrapper(call, pre="result = ")

        call = CGWrapper(call)
        self.cgRoot.append(call)

        if isFallible:
            self.cgRoot.prepend(CGGeneric("ErrorResult rv;"))
            self.cgRoot.append(CGGeneric("if (rv.Failed()) {"))
            self.cgRoot.append(CGIndenter(errorReport))
            self.cgRoot.append(CGGeneric("}"))

    def define(self):
        return self.cgRoot.define()

class MethodNotCreatorError(Exception):
    def __init__(self, typename):
        self.typename = typename

class CGPerSignatureCall(CGThing):
    """
    This class handles the guts of generating code for a particular
    call signature.  A call signature consists of four things:

    1) A return type, which can be None to indicate that there is no
       actual return value (e.g. this is an attribute setter) or an
       IDLType if there's an IDL type involved (including |void|).
    2) An argument list, which is allowed to be empty.
    3) A name of a native method to call.
    4) Whether or not this method is static.

    We also need to know whether this is a method or a getter/setter
    to do error reporting correctly.

    The idlNode parameter can be either a method or an attr. We can query
    |idlNode.identifier| in both cases, so we can be agnostic between the two.
    """
    # XXXbz For now each entry in the argument list is either an
    # IDLArgument or a FakeArgument, but longer-term we may want to
    # have ways of flagging things like JSContext* or optional_argc in
    # there.

    def __init__(self, returnType, argsPre, arguments, nativeMethodName, static,
                 descriptor, idlNode, argConversionStartsAt=0,
                 getter=False, setter=False):
        CGThing.__init__(self)
        self.returnType = returnType
        self.descriptor = descriptor
        self.idlNode = idlNode
        self.extendedAttributes = descriptor.getExtendedAttributes(idlNode,
                                                                   getter=getter,
                                                                   setter=setter)
        self.argsPre = argsPre
        self.arguments = arguments
        self.argCount = len(arguments)
        if self.argCount > argConversionStartsAt:
            # Insert our argv in there
            cgThings = [CGGeneric(self.getArgvDecl())]
        else:
            cgThings = []
        cgThings.extend([CGArgumentConverter(arguments[i], i, self.getArgv(),
                                             self.getArgc(), self.descriptor,
                                             invalidEnumValueFatal=not setter) for
                         i in range(argConversionStartsAt, self.argCount)])

        cgThings.append(CGCallGenerator(
                    self.getErrorReport() if self.isFallible() else None,
                    self.getArguments(), self.argsPre, returnType,
                    self.extendedAttributes, descriptor, nativeMethodName,
                    static))
        self.cgRoot = CGList(cgThings, "\n")

    def getArgv(self):
        return "argv" if self.argCount > 0 else ""
    def getArgvDecl(self):
        return "\nargv: *jsval = JS_ARGV(cx, vp);\n"
    def getArgc(self):
        return "argc"
    def getArguments(self):
        return [(a, "arg" + str(i)) for (i, a) in enumerate(self.arguments)]

    def isFallible(self):
        return not 'infallible' in self.extendedAttributes

    def wrap_return_value(self):
        isCreator = memberIsCreator(self.idlNode)
        if isCreator:
            # We better be returning addrefed things!
            #assert(isResultAlreadyAddRefed(self.descriptor,
            #                               self.extendedAttributes) or
                   # Workers use raw pointers for new-object return
                   # values or something
            #       self.descriptor.workers)
            pass

        resultTemplateValues = { 'jsvalRef': '*vp', 'jsvalPtr': 'vp',
                                 'isCreator': isCreator}
        try:
            return wrapForType(self.returnType, self.descriptor,
                               resultTemplateValues)
        except MethodNotCreatorError, err:
            assert not isCreator
            raise TypeError("%s being returned from non-creator method or property %s.%s" %
                            (err.typename,
                             self.descriptor.interface.identifier.name,
                             self.idlNode.identifier.name))

    def getErrorReport(self):
        return CGGeneric('return ThrowMethodFailedWithDetails<%s>(cx, rv, "%s", "%s");'
                         % (toStringBool(not self.descriptor.workers),
                            self.descriptor.interface.identifier.name,
                            self.idlNode.identifier.name))

    def define(self):
        return (self.cgRoot.define() + "\n" + self.wrap_return_value())

class CGGetterCall(CGPerSignatureCall):
    """
    A class to generate a native object getter call for a particular IDL
    getter.
    """
    def __init__(self, returnType, nativeMethodName, descriptor, attr):
        CGPerSignatureCall.__init__(self, returnType, [], [],
                                    nativeMethodName, False, descriptor,
                                    attr, getter=True)

class CGAbstractBindingMethod(CGAbstractExternMethod):
    """
    Common class to generate the JSNatives for all our methods, getters, and
    setters.  This will generate the function declaration and unwrap the
    |this| object.  Subclasses are expected to override the generate_code
    function to do the rest of the work.  This function should return a
    CGThing which is already properly indented.
    """
    def __init__(self, descriptor, name, args, unwrapFailureCode=None):
        CGAbstractExternMethod.__init__(self, descriptor, name, "JSBool", args)

        if unwrapFailureCode is None:
            self.unwrapFailureCode = ("return Throw<%s>(cx, rv);" %
                                      toStringBool(not descriptor.workers))
        else:
            self.unwrapFailureCode = unwrapFailureCode

    def definition_body(self):
        # Our descriptor might claim that we're not castable, simply because
        # we're someone's consequential interface.  But for this-unwrapping, we
        # know that we're the real deal.  So fake a descriptor here for
        # consumption by FailureFatalCastableObjectUnwrapper.
        unwrapThis = CGIndenter(CGGeneric(
            str(CastableObjectUnwrapper(
                        FakeCastableDescriptor(self.descriptor),
                        "obj", "self", self.unwrapFailureCode))))
        return CGList([ self.getThis(), unwrapThis,
                        self.generate_code() ], "\n").define()

    def getThis(self):
        return CGIndenter(
            CGGeneric("let obj: *JSObject = JS_THIS_OBJECT(cx, vp);\n"
                      "if obj.is_null() {\n"
                      "  return false as JSBool;\n"
                      "}\n"
                      "\n"
                      "let self: *%s;" % self.descriptor.nativeType))

    def generate_code(self):
        assert(False) # Override me

class CGGenericMethod(CGAbstractBindingMethod):
    """
    A class for generating the C++ code for an IDL method..
    """
    def __init__(self, descriptor):
        args = [Argument('*JSContext', 'cx'), Argument('uint', 'argc'),
                Argument('*jsval', 'vp')]
        CGAbstractBindingMethod.__init__(self, descriptor, 'genericMethod', args)

    def generate_code(self):
        return CGIndenter(CGGeneric(
            "let info: *JSJitInfo = FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx, vp));\n"
            "let method: JSJitMethodOp = (*info).op;\n"
            "return method(cx, obj, self, argc, vp);"))

class CGGenericGetter(CGAbstractBindingMethod):
    """
    A class for generating the C++ code for an IDL attribute getter.
    """
    def __init__(self, descriptor, lenientThis=False):
        args = [Argument('*JSContext', 'cx'), Argument('uint', 'argc'),
                Argument('*jsval', 'vp')]
        if lenientThis:
            name = "genericLenientGetter"
            unwrapFailureCode = (
                "MOZ_ASSERT(!JS_IsExceptionPending(cx));\n"
                "JS_SET_RVAL(cx, vp, JS::UndefinedValue());\n"
                "return true;")
        else:
            name = "genericGetter"
            unwrapFailureCode = None
        CGAbstractBindingMethod.__init__(self, descriptor, name, args,
                                         unwrapFailureCode)

    def generate_code(self):
        return CGIndenter(CGGeneric(
            "let info: *JSJitInfo = FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx, vp));\n"
            "let getter: JSJitPropertyOp = (*info).op;\n"
            "return getter(cx, obj, self, vp);"))

class CGSpecializedGetter(CGAbstractExternMethod):
    """
    A class for generating the code for a specialized attribute getter
    that the JIT can call with lower overhead.
    """
    def __init__(self, descriptor, attr):
        self.attr = attr
        name = 'get_' + attr.identifier.name
        args = [ Argument('*JSContext', 'cx'),
                 Argument('*JSObject', 'obj'),
                 Argument('*%s' % descriptor.nativeType, 'self'),
                 Argument('*mut jsval', 'vp') ]
        CGAbstractExternMethod.__init__(self, descriptor, name, "bool", args)

    def definition_body(self):
        name = self.attr.identifier.name
        nativeName = MakeNativeName(self.descriptor.binaryNames.get(name, name))
        # resultOutParam does not depend on whether resultAlreadyAddRefed is set
        (_, resultOutParam) = getRetvalDeclarationForType(self.attr.type,
                                                          self.descriptor,
                                                          False)
        infallible = ('infallible' in
                      self.descriptor.getExtendedAttributes(self.attr,
                                                            getter=True))
        if resultOutParam or self.attr.type.nullable() or not infallible:
            nativeName = "Get" + nativeName
        return CGIndenter(CGGetterCall(self.attr.type, nativeName,
                                       self.descriptor, self.attr)).define()

def infallibleForMember(member, type, descriptorProvider):
    """
    Determine the fallibility of changing a C++ value of IDL type "type" into
    JS for the given attribute. Apart from isCreator, all the defaults are used,
    since the fallbility does not change based on the boolean values,
    and the template will be discarded.

    CURRENT ASSUMPTIONS:
        We assume that successCode for wrapping up return values cannot contain
        failure conditions.
    """
    return getWrapTemplateForType(type, descriptorProvider, 'result', None,\
                                  memberIsCreator(member))[1]

class CGMemberJITInfo(CGThing):
    """
    A class for generating the JITInfo for a property that points to
    our specialized getter and setter.
    """
    def __init__(self, descriptor, member):
        self.member = member
        self.descriptor = descriptor

    def declare(self):
        return ""

    def defineJitInfo(self, infoName, opName, infallible):
        protoID = "prototypes::id::%s" % self.descriptor.name
        depth = "PrototypeTraits<%s>::Depth" % protoID
        failstr = "true" if infallible else "false"
        return ("\n"
                "const %s: JSJitInfo = {\n"
                "  op: %s,\n"
                "  protoID: %s,\n"
                "  depth: %s,\n"
                "  isInfallible: %s,  /* False in setters. */\n"
                "  isConstant: false  /* Only relevant for getters. */\n"
                "};\n" % (infoName, opName, protoID, depth, failstr))

    def define(self):
        if self.member.isAttr():
            getterinfo = ("%s_getterinfo" % self.member.identifier.name)
            getter = ("get_%s" % self.member.identifier.name)
            getterinfal = "infallible" in self.descriptor.getExtendedAttributes(self.member, getter=True)
            getterinfal = getterinfal and infallibleForMember(self.member, self.member.type, self.descriptor)
            result = self.defineJitInfo(getterinfo, getter, getterinfal)
            if not self.member.readonly:
                setterinfo = ("%s_setterinfo" % self.member.identifier.name)
                setter = ("set_%s" % self.member.identifier.name)
                # Setters are always fallible, since they have to do a typed unwrap.
                result += self.defineJitInfo(setterinfo, setter, False)
            return result
        if self.member.isMethod():
            methodinfo = ("%s_methodinfo" % self.member.identifier.name)
            # Actually a JSJitMethodOp, but JSJitPropertyOp by struct definition.
            method = ("%s" % self.member.identifier.name)

            # Methods are infallible if they are infallible, have no arguments
            # to unwrap, and have a return type that's infallible to wrap up for
            # return.
            methodInfal = False
            sigs = self.member.signatures()
            if len(sigs) == 1:
                # Don't handle overloading. If there's more than one signature,
                # one of them must take arguments.
                sig = sigs[0]
                if len(sig[1]) == 0 and infallibleForMember(self.member, sig[0], self.descriptor):
                    # No arguments and infallible return boxing
                    methodInfal = True

            result = self.defineJitInfo(methodinfo, method, methodInfal)
            return result
        raise TypeError("Illegal member type to CGPropertyJITInfo")

class CGAbstractClassHook(CGAbstractExternMethod):
    """
    Meant for implementing JSClass hooks, like Finalize or Trace. Does very raw
    'this' unwrapping as it assumes that the unwrapped type is always known.
    """
    def __init__(self, descriptor, name, returnType, args):
        CGAbstractExternMethod.__init__(self, descriptor, name, returnType,
                                        args)

    def definition_body_prologue(self):
        return """
  %s* self = unwrap<%s>(obj);
""" % (self.descriptor.nativeType, self.descriptor.nativeType)

    def definition_body(self):
        return self.definition_body_prologue() + self.generate_code()

    def generate_code(self):
        # Override me
        assert(False)

def finalizeHook(descriptor, hookName, context):
    if descriptor.customFinalize:
        return """if (self) {
  self->%s(%s);
}""" % (hookName, context)
    #clearWrapper = "ClearWrapper(self, self);\n" if descriptor.wrapperCache else ""
    if descriptor.workers:
        release = "self->Release();"
    else:
        assert descriptor.nativeIsISupports
        release = """let val = JS_GetReservedSlot(obj, 0);
let _: %s = cast::reinterpret_cast(&RUST_JSVAL_TO_PRIVATE(val));
""" % (descriptor.pointerType + descriptor.nativeType)
    #return clearWrapper + release
    return release

class CGClassFinalizeHook(CGAbstractClassHook):
    """
    A hook for finalize, used to release our native object.
    """
    def __init__(self, descriptor):
        args = [Argument('*JSFreeOp', 'fop'), Argument('*JSObject', 'obj')]
        CGAbstractClassHook.__init__(self, descriptor, FINALIZE_HOOK_NAME,
                                     'void', args)

    def generate_code(self):
        return CGIndenter(CGGeneric(finalizeHook(self.descriptor, self.name, self.args[0].name))).define()

class CGDescriptor(CGThing):
    def __init__(self, descriptor):
        CGThing.__init__(self)

        assert not descriptor.concrete or descriptor.interface.hasInterfacePrototypeObject()

        cgThings = []
        if descriptor.interface.hasInterfacePrototypeObject():
            (hasMethod, hasGetter, hasLenientGetter,
             hasSetter, hasLenientSetter) = False, False, False, False, False
            for m in descriptor.interface.members:
                if m.isMethod() and not m.isStatic() and not m.isIdentifierLess():
                    #cgThings.append(CGSpecializedMethod(descriptor, m))
                    #cgThings.append(CGMemberJITInfo(descriptor, m))
                    hasMethod = True
                elif m.isAttr():
                    cgThings.append(CGSpecializedGetter(descriptor, m))
                    if m.hasLenientThis():
                        hasLenientGetter = True
                    else:
                        hasGetter = True
                    if not m.readonly:
                        #cgThings.append(CGSpecializedSetter(descriptor, m))
                        if m.hasLenientThis():
                            hasLenientSetter = True
                        else:
                            hasSetter = True
                    cgThings.append(CGMemberJITInfo(descriptor, m))
            #if hasMethod: cgThings.append(CGGenericMethod(descriptor))
            if hasGetter: cgThings.append(CGGenericGetter(descriptor))
            #if hasLenientGetter: cgThings.append(CGGenericGetter(descriptor,
            #                                                     lenientThis=True))
            #if hasSetter: cgThings.append(CGGenericSetter(descriptor))
            #if hasLenientSetter: cgThings.append(CGGenericSetter(descriptor,
            #                                                     lenientThis=True))

        if descriptor.concrete and not descriptor.proxy:
            if not descriptor.workers and descriptor.wrapperCache:
                #cgThings.append(CGAddPropertyHook(descriptor))
                pass

            # Always have a finalize hook, regardless of whether the class wants a
            # custom hook.
            cgThings.append(CGClassFinalizeHook(descriptor))

            # Only generate a trace hook if the class wants a custom hook.
            if (descriptor.customTrace):
                #cgThings.append(CGClassTraceHook(descriptor))
                pass

        if descriptor.interface.hasInterfaceObject():
            #cgThings.append(CGClassConstructHook(descriptor))
            #cgThings.append(CGClassHasInstanceHook(descriptor))
            #cgThings.append(CGInterfaceObjectJSClass(descriptor))
            pass

        if descriptor.interface.hasInterfacePrototypeObject():
            #cgThings.append(CGPrototypeJSClass(descriptor))
            pass

        properties = PropertyArrays(descriptor)
        #cgThings.append(CGGeneric(define=str(properties)))
        #cgThings.append(CGCreateInterfaceObjectsMethod(descriptor, properties))
        if descriptor.interface.hasInterfacePrototypeObject():
            #cgThings.append(CGGetProtoObjectMethod(descriptor))
            pass
        else:
            #cgThings.append(CGGetConstructorObjectMethod(descriptor))
            pass

        # Set up our Xray callbacks as needed.  Note that we don't need to do
        # it in workers.
        if (descriptor.interface.hasInterfacePrototypeObject() and
            not descriptor.workers):
            if descriptor.concrete and descriptor.proxy:
                #cgThings.append(CGResolveOwnProperty(descriptor))
                #cgThings.append(CGEnumerateOwnProperties(descriptor))
                pass
            #cgThings.append(CGResolveProperty(descriptor, properties))
            #cgThings.append(CGEnumerateProperties(descriptor, properties))

        if descriptor.interface.hasInterfaceObject():
            #cgThings.append(CGDefineDOMInterfaceMethod(descriptor))
            if (not descriptor.interface.isExternal() and
                # Workers stuff is never pref-controlled
                not descriptor.workers and
                descriptor.interface.getExtendedAttribute("PrefControlled") is not None):
                #cgThings.append(CGPrefEnabled(descriptor))
                pass

        if descriptor.interface.hasInterfacePrototypeObject():
            #cgThings.append(CGNativePropertyHooks(descriptor))
            pass

        if descriptor.concrete:
            if descriptor.proxy:
                #cgThings.append(CGProxyIsProxy(descriptor))
                #cgThings.append(CGProxyUnwrap(descriptor))
                #cgThings.append(CGDOMJSProxyHandlerDOMClass(descriptor))
                #cgThings.append(CGDOMJSProxyHandler(descriptor))
                #cgThings.append(CGIsMethod(descriptor))
                pass
            else:
                #cgThings.append(CGDOMJSClass(descriptor))
                pass

            if descriptor.wrapperCache:
                #cgThings.append(CGWrapWithCacheMethod(descriptor))
                #cgThings.append(CGWrapMethod(descriptor))
                pass
            else:
                #cgThings.append(CGWrapNonWrapperCacheMethod(descriptor))
                pass

        cgThings = CGList((CGIndenter(t, declareOnly=True) for t in cgThings), "\n")
        cgThings = CGWrapper(cgThings, pre='\n', post='\n')
        #self.cgRoot = CGWrapper(CGNamespace(toBindingNamespace(descriptor.name),
        #                                    cgThings),
        #                        post='\n')
        self.cgRoot = cgThings

    def declare(self):
        return self.cgRoot.declare()
    def define(self):
        return self.cgRoot.define()

class CGBindingRoot(CGThing):
    """
    Root codegen class for binding generation. Instantiate the class, and call
    declare or define to generate header or cpp code (respectively).
    """
    def __init__(self, config, prefix, webIDLFile):
        descriptors = config.getDescriptors(webIDLFile=webIDLFile,
                                            hasInterfaceOrInterfacePrototypeObject=True)
        dictionaries = config.getDictionaries(webIDLFile)

        cgthings = []

        # Do codegen for all the descriptors
        cgthings.extend([CGDescriptor(x) for x in descriptors])

        # And make sure we have the right number of newlines at the end
        curr = CGWrapper(CGList(cgthings, "\n\n"), post="\n\n")

        # Wrap all of that in our namespaces.
        #curr = CGNamespace.build(['dom'],
        #                         CGWrapper(curr, pre="\n"))

        # Add imports
        curr = CGImports(descriptors,
                         dictionaries,
                         [],
                         ['js::*',
                          'js::jsapi::*',
                          'js::jsapi::bindgen::*',
                          'js::glue::bindgen::*',
                          'dom::bindings::utils::*'],
                         curr)

        # Add the auto-generated comment.
        curr = CGWrapper(curr, pre=AUTOGENERATED_WARNING_COMMENT)

        # Store the final result.
        self.root = curr
    def declare(self):
        return stripTrailingWhitespace(self.root.declare())
    def define(self):
        return stripTrailingWhitespace(self.root.define())