medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

generator.py (28937B)


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
from itertools import chain
import os.path
import sys

from glad.lang.common.generator import Generator
from glad.lang.common.util import makefiledir


if sys.version_info >= (3, 0):
    from io import StringIO
    basestring = str
else:
    from StringIO import StringIO


def _gl_types(gen, f):
    gen.write_opaque_struct(f, '__GLsync')
    gen.write_alias(f, 'GLsync', '__GLsync*')
    gen.write_opaque_struct(f, '_cl_context')
    gen.write_opaque_struct(f, '_cl_event')
    gen.write_extern(f)
    gen.write_alias(
        f,
        'GLDEBUGPROC', 'void function(GLenum, GLenum, '
        'GLuint, GLenum, GLsizei, in GLchar*, GLvoid*)'
    )
    gen.write_alias(f, 'GLDEBUGPROCARB', 'GLDEBUGPROC')
    gen.write_alias(f, 'GLDEBUGPROCKHR', 'GLDEBUGPROC')
    gen.write_alias(
        f,
        'GLDEBUGPROCAMD', 'void function(GLuint, GLenum, '
        'GLenum, GLsizei, in GLchar*, GLvoid*)'
    )
    gen.write_extern_end(f)


def _egl_types(gen, f):
    io = StringIO()
    gen.write_opaque_struct(io, 'egl_native_pixmap_t')

    f.write('''
// Thanks to @jpf91 (github) for these declarations
version(Windows) {
    import core.sys.windows.windows;
    alias EGLNativeDisplayType = HDC;
    alias EGLNativePixmapType = HBITMAP;
    alias EGLNativeWindowType = HWND;
} else version(Symbian) {
    alias EGLNativeDisplayType = int;
    alias EGLNativeWindowType = void*;
    alias EGLNativePixmapType = void*;
} else version(Android) {
    //import android.native_window;
    //struct egl_native_pixmap_t;
    ''' + io.getvalue() + '''
    //alias ANativeWindow*           EGLNativeWindowType;
    //alias egl_native_pixmap_t*     EGLNativePixmapType;
    alias EGLNativeWindowType = void*;
    alias EGLNativePixmapType = void*;
    alias EGLNativeDisplayType = void*;
} else version(linux) {
    version(Xlib) {
        import X11.Xlib;
        import X11.Xutil;
        alias EGLNativeDisplayType = Display*;
        alias EGLNativePixmapType = Pixmap;
        alias EGLNativeWindowType = Window;
    } else {
        alias EGLNativeDisplayType = void*;
        alias EGLNativePixmapType = uint;
        alias EGLNativeWindowType = uint;
    }
}
alias EGLObjectKHR = void*;
alias EGLLabelKHR = void*;

extern(System) {
alias EGLSetBlobFuncANDROID = void function(const(void)*, EGLsizeiANDROID, const(void)*, EGLsizeiANDROID);
alias EGLGetBlobFuncANDROID = EGLsizeiANDROID function(const(void)*, EGLsizeiANDROID, const(void)* EGLsizeiANDROID);
struct EGLClientPixmapHI {
    void  *pData;
    EGLint iWidth;
    EGLint iHeight;
    EGLint iStride;
}
alias EGLDEBUGPROCKHR = void function(EGLenum error,const char *command,EGLint messageType,EGLLabelKHR threadLabel,EGLLabelKHR objectLabel,const char* message);
}
''')
    gen.write_extern(f)
    gen.write_opaque_struct(f, '_cl_event')
    gen.write_extern_end(f)


def _glx_types(gen, f):
    f.write('''
version(Xlib) {
    import X11.Xlib;
    import X11.Xutil;
} else {
    alias Bool = int;
    alias Status = int;
    alias VisualID = uint;
    alias XPointer = byte*;
    alias XID = uint;
    alias Colormap = XID;
    alias Display = void;
    alias Font = XID;
    alias Window = XID;
    alias Drawable = XID;
    alias Pixmap = XID;
    alias Cursor = XID;
    alias GContext = XID;
    alias KeySym = XID;

    extern(System) {
        // Borrowed from derelict
        struct XExtData {
            int number;
            XExtData* next;
            extern(C) int function(XExtData*) free_private;
            XPointer private_data;
        }

        struct Visual {
            XExtData* ext_data;
            VisualID  visualid;
            int       _class;
            uint      red_mask, green_mask, blue_mask;
            int       bits_per_rgb;
            int       map_entries;
        }

        struct XVisualInfo {
            Visual   *visual;
            VisualID visualid;
            int      screen;
            int      depth;
            int      _class;
            uint     red_mask;
            uint     green_mask;
            uint     blue_mask;
            int      colormap_size;
            int      bits_per_rgb;
        }
    }
}

alias DMbuffer = void*;
alias DMparams = void*;
alias VLNode = void*;
alias VLPath = void*;
alias VLServer = void*;

alias int64_t = long;
alias uint64_t = ulong;
alias int32_t = int;

alias GLXContextID = uint;
alias GLXPixmap = uint;
alias GLXDrawable = uint;
alias GLXPbuffer = uint;
alias GLXWindow = uint;
alias GLXFBConfigID = uint;
alias GLXVideoCaptureDeviceNV = XID;
alias GLXPbufferSGIX = XID;
alias GLXVideoSourceSGIX = XID;
alias GLXVideoDeviceNV = uint;


extern(System) {
    alias __GLXextFuncPtr = void function();

    struct GLXPbufferClobberEvent {
        int event_type;             /* GLX_DAMAGED or GLX_SAVED */
        int draw_type;              /* GLX_WINDOW or GLX_PBUFFER */
        ulong serial;       /* # of last request processed by server */
        Bool send_event;            /* true if this came for SendEvent request */
        Display *display;           /* display the event was read from */
        GLXDrawable drawable;       /* XID of Drawable */
        uint buffer_mask;   /* mask indicating which buffers are affected */
        uint aux_buffer;    /* which aux buffer was affected */
        int x, y;
        int width, height;
        int count;                  /* if nonzero, at least this many more */
    }

    struct GLXBufferSwapComplete {
        int type;
        ulong serial;       /* # of last request processed by server */
        Bool send_event;            /* true if this came from a SendEvent request */
        Display *display;           /* Display the event was read from */
        GLXDrawable drawable;       /* drawable on which event was requested in event mask */
        int event_type;
        long ust;
        long msc;
        long sbc;
    }

    union GLXEvent {
        GLXPbufferClobberEvent glxpbufferclobber;
        GLXBufferSwapComplete glxbufferswapcomplete;
        int[24] pad;
    }

    struct GLXBufferClobberEventSGIX {
        int type;
        ulong serial;   /* # of last request processed by server */
        Bool send_event;        /* true if this came for SendEvent request */
        Display *display;       /* display the event was read from */
        GLXDrawable drawable;   /* i.d. of Drawable */
        int event_type;         /* GLX_DAMAGED_SGIX or GLX_SAVED_SGIX */
        int draw_type;          /* GLX_WINDOW_SGIX or GLX_PBUFFER_SGIX */
        uint mask;      /* mask indicating which buffers are affected*/
        int x, y;
        int width, height;
        int count;              /* if nonzero, at least this many more */
    }

    struct GLXHyperpipeNetworkSGIX {
        char[80] pipeName; /* Should be [GLX_HYPERPIPE_PIPE_NAME_LENGTH_SGIX] */
        int      networkId;
    }

    struct GLXHyperpipeConfigSGIX {
        char[80] pipeName; /* Should be [GLX_HYPERPIPE_PIPE_NAME_LENGTH_SGIX] */
        int      channel;
        uint     participationType;
        int      timeSlice;
    }

    struct GLXPipeRect {
        char[80] pipeName; /* Should be [GLX_HYPERPIPE_PIPE_NAME_LENGTH_SGIX] */
        int srcXOrigin, srcYOrigin, srcWidth, srcHeight;
        int destXOrigin, destYOrigin, destWidth, destHeight;
    }

    struct GLXPipeRectLimits {
        char[80] pipeName; /* Should be [GLX_HYPERPIPE_PIPE_NAME_LENGTH_SGIX] */
        int XOrigin, YOrigin, maxHeight, maxWidth;
    }
}
''')
    gen.write_extern(f)
    gen.write_opaque_struct(f, '__GLXcontextRec')
    gen.write_alias(f, 'GLXContext', '__GLXcontextRec*')
    gen.write_opaque_struct(f, '__GLXFBConfigRec')
    gen.write_alias(f, 'GLXFBConfig', '__GLXFBConfigRec*')
    gen.write_alias(f, 'GLXFBConfigSGIX', '__GLXFBConfigRec*')
    gen.write_extern_end(f)


def _wgl_types(gen, f):
    f.write('''
version(Windows) {
    public import core.sys.windows.windows;
} else {
    alias BOOL = int;
    alias CHAR = char;
    alias WORD = ushort;
    alias DWORD = uint;
    alias FLOAT = float;
    alias HANDLE = void*;
    alias HDC = HANDLE;
    alias HGLRC = HANDLE;
    alias INT = int;
    alias LPCSTR = const(CHAR)*;
    alias LPVOID = void*;
    alias UINT = uint;
    alias USHORT = ushort;
    alias VOID = void;
    alias COLORREF = DWORD;
    alias HENHMETAFILE = HANDLE;
    alias BYTE = byte;
}

alias PROC = HANDLE;

extern(System) {
    struct RECT {
        int left;
        int top;
        int right;
        int bottom;
    }

    struct LAYERPLANEDESCRIPTOR {
        WORD     nSize;
        WORD     nVersion;
        DWORD    dwFlags;
        BYTE     iPixelType;
        BYTE     cColorBits;
        BYTE     cRedBits;
        BYTE     cRedShift;
        BYTE     cGreenBits;
        BYTE     cGreenShift;
        BYTE     cBlueBits;
        BYTE     cBlueShift;
        BYTE     cAlphaBits;
        BYTE     cAlphaShift;
        BYTE     cAccumBits;
        BYTE     cAccumRedBits;
        BYTE     cAccumGreenBits;
        BYTE     cAccumBlueBits;
        BYTE     cAccumAlphaBits;
        BYTE     cDepthBits;
        BYTE     cStencilBits;
        BYTE     cAuxBuffers;
        BYTE     iLayerType;
        BYTE     bReserved;
        COLORREF crTransparent;
    }

    struct PIXELFORMATDESCRIPTOR {
        WORD  nSize;
        WORD  nVersion;
        DWORD dwFlags;
        BYTE  iPixelType;
        BYTE  cColorBits;
        BYTE  cRedBits;
        BYTE  cRedShift;
        BYTE  cGreenBits;
        BYTE  cGreenShift;
        BYTE  cBlueBits;
        BYTE  cBlueShift;
        BYTE  cAlphaBits;
        BYTE  cAlphaShift;
        BYTE  cAccumBits;
        BYTE  cAccumRedBits;
        BYTE  cAccumGreenBits;
        BYTE  cAccumBlueBits;
        BYTE  cAccumAlphaBits;
        BYTE  cDepthBits;
        BYTE  cStencilBits;
        BYTE  cAuxBuffers;
        BYTE  iLayerType;
        BYTE  bReserved;
        DWORD dwLayerMask;
        DWORD dwVisibleMask;
        DWORD dwDamageMask;
    }

    struct POINTFLOAT {
        FLOAT x;
        FLOAT y;
    }

    struct GLYPHMETRICSFLOAT {
        FLOAT      gmfBlackBoxX;
        FLOAT      gmfBlackBoxY;
        POINTFLOAT gmfptGlyphOrigin;
        FLOAT      gmfCellIncX;
        FLOAT      gmfCellIncY;
    }
    alias PGLYPHMETRICSFLOAT = GLYPHMETRICSFLOAT*;
    alias LPGLYPHMETRICSFLOAT = GLYPHMETRICSFLOAT;

    struct GPU_DEVICE {
        DWORD      cb;
        CHAR[32]   DeviceName;
        CHAR[128]  DeviceString;
        DWORD      Flags;
        RECT       rcVirtualScreen;
    }

    alias PGPU_DEVICE = GPU_DEVICE;
}
''')
    gen.write_opaque_struct(f, 'HPBUFFERARB')
    gen.write_opaque_struct(f, 'HPBUFFEREXT')
    gen.write_opaque_struct(f, 'HVIDEOOUTPUTDEVICENV')
    gen.write_opaque_struct(f, 'HPVIDEODEV')
    gen.write_opaque_struct(f, 'HPGPUNV')
    gen.write_opaque_struct(f, 'HGPUNV')
    gen.write_opaque_struct(f, 'HVIDEOINPUTDEVICENV')


DTYPES = {
    '__pre': {
        'egl': 'import core.stdc.stdint : intptr_t;\n\n'
    },

    '__other': {
        'gl': _gl_types,
        'egl': _egl_types,
        'glx': _glx_types,
        'wgl': _wgl_types
    },

    'gl': {
        'GLenum': 'uint', 'GLvoid': 'void', 'GLboolean': 'ubyte',
        'GLbitfield': 'uint', 'GLchar': 'char', 'GLbyte': 'byte',
        'GLshort': 'short', 'GLint': 'int', 'GLclampx': 'int',
        'GLsizei': 'int', 'GLubyte': 'ubyte', 'GLushort': 'ushort',
        'GLuint': 'uint', 'GLhalf': 'ushort', 'GLfloat': 'float',
        'GLclampf': 'float', 'GLdouble': 'double', 'GLclampd': 'double',
        'GLfixed': 'int', 'GLintptr': 'ptrdiff_t', 'GLsizeiptr': 'ptrdiff_t',
        'GLintptrARB': 'ptrdiff_t', 'GLsizeiptrARB': 'ptrdiff_t',
        'GLcharARB': 'byte', 'GLhandleARB': 'uint', 'GLhalfARB': 'ushort',
        'GLhalfNV': 'ushort', 'GLint64EXT': 'long', 'GLuint64EXT': 'ulong',
        'GLint64': 'long', 'GLuint64': 'ulong',
        'GLvdpauSurfaceNV': 'ptrdiff_t', 'GLeglImageOES': 'void*'
    },
    'egl': {
        'EGLBoolean': 'uint', 'EGLenum': 'uint', 'EGLAttribKHR': 'intptr_t',
        'EGLAttrib': 'intptr_t', 'EGLClientBuffer': 'void*', 'EGLConfig': 'void*',
        'EGLContext': 'void*', 'EGLDeviceEXT': 'void*', 'EGLDisplay': 'void*',
        'EGLImage': 'void*', 'EGLImageKHR': 'void*', 'EGLOutputLayerEXT': 'void*',
        'EGLOutputPortEXT': 'void*', 'EGLStreamKHR': 'void*', 'EGLSurface': 'void*',
        'EGLSync': 'void*', 'EGLSyncKHR': 'void*', 'EGLSyncNV': 'void*',
        '__eglMustCastToProperFunctionPointerType': 'void function()',
        'EGLint': 'int', 'EGLTimeKHR': 'ulong', 'EGLTime': 'ulong',
        'EGLTimeNV': 'ulong', 'EGLuint64NV': 'ulong',
        'EGLuint64KHR': 'ulong', 'EGLuint64MESA': 'ulong',
        'EGLsizeiANDROID': 'ptrdiff_t', 'EGLNativeFileDescriptorKHR': 'int'
    },
    'glx': {
        'GLboolean': 'ubyte', 'GLenum': 'uint', 'GLint': 'int',
        'GLsizei': 'int', 'GLubyte': 'ubyte', 'GLuint': 'uint',
        'GLfloat': 'float', 'GLbitfield': 'uint', 'GLintptr': 'ptrdiff_t',
        'GLsizeiptr': 'ptrdiff_t'

    },
    'wgl': {
        'GLbitfield': 'uint', 'GLenum': 'uint', 'GLfloat': 'float',
        'GLint': 'int', 'GLsizei': 'int', 'GLuint': 'uint',
        'GLushort': 'ushort', 'INT32': 'int', 'INT64': 'long',
        'GLboolean': 'ubyte'
    },

    'SpecialNumbers': {
        'gl': [
            ('GL_FALSE', '0', 'ubyte'), ('GL_TRUE', '1', 'ubyte'),
            ('GL_NO_ERROR', '0', 'uint'), ('GL_NONE', '0', 'uint'),
            ('GL_ZERO', '0', 'uint'), ('GL_ONE', '1', 'uint'),
            ('GL_NONE_OES', '0', 'uint'),
            ('GL_INVALID_INDEX', '0xFFFFFFFF', 'uint'),
            ('GL_TIMEOUT_IGNORED', '0xFFFFFFFFFFFFFFFF', 'ulong'),
            ('GL_TIMEOUT_IGNORED_APPLE', '0xFFFFFFFFFFFFFFFF', 'ulong'),
            ('GL_VERSION_ES_CL_1_0', '1', 'uint'), ('GL_VERSION_ES_CM_1_1', '1', 'uint'),
            ('GL_VERSION_ES_CL_1_1', '1', 'uint')
        ],
        'egl': [
            ('EGL_DONT_CARE', '-1', 'int'), ('EGL_UNKNOWN', '-1', 'int'),
            ('EGL_NO_NATIVE_FENCE_FD_ANDROID', '-1', 'uint'),
            ('EGL_DEPTH_ENCODING_NONE_NV', '0', 'uint'),
            ('EGL_NO_CONTEXT', 'cast(EGLContext)0', 'EGLContext'),
            ('EGL_NO_DEVICE_EXT', 'cast(EGLDeviceEXT)0', 'EGLDeviceEXT'),
            ('EGL_NO_DISPLAY', 'cast(EGLDisplay)0', 'EGLDisplay'),
            ('EGL_NO_IMAGE', 'cast(EGLImage)0', 'EGLImage'),
            ('EGL_NO_IMAGE_KHR', 'cast(EGLImageKHR)0', 'EGLImageKHR'),
            ('EGL_DEFAULT_DISPLAY', 'cast(EGLNativeDisplayType)0', 'EGLNativeDisplayType'),
            ('EGL_NO_FILE_DESCRIPTOR_KHR', 'cast(EGLNativeFileDescriptorKHR)-1', 'EGLNativeFileDescriptorKHR'),
            ('EGL_NO_OUTPUT_LAYER_EXT', 'cast(EGLOutputLayerEXT)0', 'EGLOutputLayerEXT'),
            ('EGL_NO_OUTPUT_PORT_EXT', 'cast(EGLOutputPortEXT)0', 'EGLOutputPortEXT'),
            ('EGL_NO_STREAM_KHR', 'cast(EGLStreamKHR)0', 'EGLStreamKHR'),
            ('EGL_NO_SURFACE', 'cast(EGLSurface)0', 'EGLSurface'),
            ('EGL_NO_SYNC', 'cast(EGLSync)0', 'EGLSync'),
            ('EGL_NO_SYNC_KHR', 'cast(EGLSyncKHR)0', 'EGLSyncKHR'),
            ('EGL_NO_SYNC_NV', 'cast(EGLSyncNV)0', 'EGLSyncNV'),
            ('EGL_DISPLAY_SCALING', '10000', 'uint'),
            ('EGL_FOREVER', '0xFFFFFFFFFFFFFFFF', 'ulong'),
            ('EGL_FOREVER_KHR', '0xFFFFFFFFFFFFFFFF', 'ulong'),
            ('EGL_FOREVER_NV', '0xFFFFFFFFFFFFFFFF', 'ulong')
        ],
        'glx': [
            ('GLX_DONT_CARE', '0xFFFFFFFF', 'uint'),
            ('GLX_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB', '0', 'uint')
        ],
        'wgl': [
            ('WGL_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB', '0', 'uint'),
            ('WGL_FONT_LINES', '0', 'uint'),
            ('WGL_FONT_POLYGONS', 1, 'uint')
        ]
    }
}


class BaseDGenerator(Generator):
    NAME = 'd'
    NAME_LONG = 'D'

    def open(self):
        self._f_loader = open(self.make_path(self.LOADER), 'w')
        self._f_gl = open(self.make_path(self.PACKAGE), 'w')
        self._f_types = open(self.make_path(self.TYPES), 'w')
        self._f_enums = open(self.make_path(self.ENUMS), 'w')
        self._f_funcs = open(self.make_path(self.FUNCS), 'w')
        self._f_exts = open(self.make_path(self.EXT), 'w')

    def close(self):
        self._f_loader.close()
        self._f_gl.close()
        self._f_types.close()
        self._f_enums.close()
        self._f_funcs.close()
        self._f_exts.close()

    @property
    def PACKAGE(self):
        return 'all'

    def generate_header(self):
        self._f_gl.write('/*\n')
        self._f_gl.write(self.header)
        self._f_gl.write('*/\n\n')

    def generate_loader(self, features, extensions):
        f = self._f_loader

        rfeatures = features
        if self.spec.NAME in ('egl', 'wgl'):
            features = {'egl': [], 'wgl': []}

        self.write_module(f, self.LOADER)
        self.write_imports(f, [self.FUNCS, self.EXT, self.ENUMS, self.TYPES])

        self.loader.write(f)
        self.loader.write_has_ext(f)

        written = set()
        for api, version in self.api.items():
            loadername = 'Load' if self.LOAD_GL_PREFIX else 'load'
            f.write('bool {}{}{}(Loader load) {{\n'
                    .format(self.LOAD_GL_PREFIX, loadername, api.upper()))
            self.loader.write_begin_load(f)
            f.write('\tfind_core{}();\n'.format(api.upper()))
            for feature in features[api]:
                f.write('\tload_{}(load);\n'.format(feature.name))
            f.write('\n\tfind_extensions{}();\n'.format(api.upper()))
            for ext in extensions[api]:
                if len(list(ext.functions)) == 0:
                    continue
                f.write('\tload_{}(load);\n'.format(ext.name))
            self.loader.write_end_load(f)
            f.write('}\n\n')

            f.write('private {\n\n')

            f.write('void find_core{}() {{\n'.format(api.upper()))
            self.loader.write_find_core(f)
            if self.spec.NAME == 'gl':
                for feature in features[api]:
                    f.write('\t{} = (major == {num[0]} && minor >= {num[1]}) ||'
                        ' major > {num[0]};\n'.format(feature.name, num=feature.number))
            f.write('\treturn;\n')
            f.write('}\n\n')

            f.write('void find_extensions{}() {{\n'.format(api.upper()))
            if self.spec.NAME == 'gl':
                for ext in extensions[api]:
                    f.write('\t{0} = has_ext("{0}");\n'.format(ext.name))
            f.write('\treturn;\n')
            f.write('}\n\n')

            for feature in features[api]:
                f.write('void load_{}(Loader load) {{\n'
                            .format(feature.name))
                if self.spec.NAME == 'gl':
                    f.write('\tif(!{}) return;\n'.format(feature.name))
                for func in feature.functions:
                    f.write('\t{name} = cast(typeof({name}))load("{name}");\n'
                        .format(name=func.proto.name))
                f.write('\treturn;\n}\n\n')

            for ext in extensions[api]:
                if len(list(ext.functions)) == 0 or ext.name in written:
                    continue

                f.write('void load_{}(Loader load) {{\n'
                    .format(ext.name))
                if self.spec.NAME == 'gl':
                    f.write('\tif(!{}) return;\n'.format(ext.name))
                for func in ext.functions:
                    # even if they were in written we need to load it
                    f.write('\t{name} = cast(typeof({name}))load("{name}");\n'
                        .format(name=func.proto.name))
                f.write('\treturn;\n')
                f.write('}\n')

                written.add(ext.name)

            f.write('\n} /* private */\n\n')

        self.write_packages(rfeatures, extensions)

    def write_packages(self, allfeatures, allextensions):
        f = self._f_gl

        self.write_module(f, self.PACKAGE)
        self.write_imports(f, [self.FUNCS, self.EXT, self.ENUMS, self.TYPES], False)

        for api, features in allfeatures.items():
            extensions = allextensions[api]
            with open(self.make_path(api), 'w') as f:
                self.write_module(f, api)

                self.write_imports(f, [self.TYPES], False)

                extenums = chain.from_iterable(ext.enums for ext in extensions)
                funcenums = chain.from_iterable(ext.enums for ext in extensions)
                enums = set(enum.name for enum in extenums) | \
                        set(enum.name for enum in funcenums)

                featfuncs = set(func.proto.name for func in
                        chain.from_iterable(feat.functions for feat in features))
                extfuncs = set(func.proto.name for func in
                        chain.from_iterable(ext.functions for ext in extensions))
                extfuncs = extfuncs - featfuncs

                enums |= set(enum.name for enum in
                        chain.from_iterable(feat.enums for feat in features))

                self.write_selective_import(f, self.FUNCS, featfuncs)
                self.write_selective_import(f, self.EXT, extfuncs)
                self.write_selective_import(f, self.ENUMS, enums)


    def generate_types(self, types):
        f = self._f_types

        self.write_module(f, self.TYPES)

        f.write(self.TYPE_DICT.get('__pre', {}).get(self.spec.NAME,''))
        for ogl, d in self.TYPE_DICT[self.spec.NAME].items():
            self.write_alias(f, ogl, d)
        self.TYPE_DICT['__other'][self.spec.NAME](self, f)

    def generate_features(self, features):
        self.write_enums(features)
        self.write_funcs(features)

    def write_enums(self, features):
        e = self._f_enums

        self.write_module(e, self.ENUMS)
        self.write_imports(e, [self.TYPES])

        for v in self.TYPE_DICT['SpecialNumbers'][self.spec.NAME]:
            self.write_enum(e, *v)

        written = set()
        for feature in features:
            for enum in feature.enums:
                if enum.group == 'SpecialNumbers':
                    written.add(enum)
                    continue
                if not enum in written:
                    self.write_enum(e, enum.name, enum.value)
                written.add(enum)

    def write_funcs(self, features):
        f = self._f_funcs

        self.write_module(f, self.FUNCS)
        self.write_imports(f, [self.TYPES])

        if self.spec.NAME == 'gl':
            for feature in features:
                self.write_boolean(f, feature.name)

        if self.spec.NAME in ('egl', 'wgl'):
            self.write_extern(f)
            for feature in features:
                for func in feature.functions:
                    self.write_function_def(f, func)
            self.write_extern_end(f)
        else:
            self.write_functions(f, set(), set(), features)

    def generate_extensions(self, extensions, enums, functions):
        f = self._f_exts

        self.write_module(f, self.EXT)
        self.write_imports(f, [self.TYPES, self.ENUMS, self.FUNCS])

        write = set()
        written = set(enum.name for enum in enums) | \
                    set(function.proto.name for function in functions)
        for ext in extensions:
            if self.spec.NAME == 'gl' and not ext.name in written:
                self.write_boolean(f, ext.name)
            for enum in ext.enums:
                if not enum.name in written and not enum.group == 'SpecialNumbers':
                    self.write_enum(self._f_enums, enum.name, enum.value)
                written.add(enum.name)
            written.add(ext.name)

        self.write_functions(f, write, written, extensions)

    def write_functions(self, f, write, written, extensions):
        self.write_prototype_pre(f)
        for ext in extensions:
            for func in ext.functions:
                if not func.proto.name in written:
                    self.write_function_prototype(f, func)
                    write.add(func)
                written.add(func.proto.name)
        self.write_prototype_post(f)

        self.write_function_pre(f)
        for func in write:
            self.write_function(f, func)
        self.write_function_post(f)


    def make_path(self, name):
        path = os.path.join(self.path, self.MODULE.split('.')[-1],
                            self.spec.NAME, name.split('.')[-1] + self.FILE_EXTENSION)
        makefiledir(path)
        return path

    def write_imports(self, fobj, modules, private=True):
        raise NotImplementedError

    def write_selective_import(self, fobj, imports):
        raise NotImplementedError

    def write_module(self, fobj, name):
        raise NotImplementedError

    def write_prototype_pre(self, fobj):
        raise NotImplementedError

    def write_prototype_post(self, fobj):
        raise NotImplementedError

    def write_function_pre(self, fobj):
        raise NotImplementedError

    def write_function_post(self, fobj):
        raise NotImplementedError

    def write_extern(self, fobj):
        raise NotImplementedError

    def write_extern_end(self, fobj):
        raise NotImplementedError

    def write_shared(self, fobj):
        raise NotImplementedError

    def write_shared_end(self, fobj):
        raise NotImplementedError

    def write_function_def(self, fobj, func):
        raise NotImplementedError

    def write_function(self, fobj, func):
        raise NotImplementedError

    def write_function_prototype(self, fobj, func):
        raise NotImplementedError

    def write_boolean(self, fobj, name, value=False):
        raise NotImplementedError

    def write_enum(self, fobj, name, value, type='uint'):
        raise NotImplementedError

    def write_opaque_struct(self, fobj, name):
        raise NotImplementedError

    def write_alias(self, fobj, newn, decl):
        raise NotImplementedError


class DGenerator(BaseDGenerator):
    MODULE = 'glad'
    LOADER = 'loader'
    ENUMS = 'enums'
    EXT = 'ext'
    FUNCS = 'funcs'
    TYPES = 'types'
    FILE_EXTENSION = '.d'
    TYPE_DICT = DTYPES

    LOAD_GL_PREFIX = 'glad'

    def write_imports(self, fobj, modules, private=True):
        for mod in modules:
            if private:
                fobj.write('private ')
            else:
                fobj.write('public ')

            fobj.write('import {}.{}.{};\n'.format(self.MODULE, self.spec.NAME, mod))

    def write_selective_import(self, fobj, mod, imports):
        if len(imports) == 0: return

        fobj.write('public import {}.{}.{} :\n'.format(self.MODULE, self.spec.NAME, mod))
        imports = set(imports)
        last = len(imports)
        for i, im in enumerate(imports, 1):
            fobj.write(im)
            if not i == last:
                fobj.write(', ')
            if (i % 5) == 0:
                fobj.write('\n')
        fobj.write(';\n\n')

    def write_module(self, fobj, name):
        fobj.write('module {}.{}.{};\n\n\n'.format(self.MODULE, self.spec.NAME, name))

    def write_prototype_pre(self, fobj):
        fobj.write('nothrow @nogc ')
        self.write_extern(fobj)

    def write_prototype_post(self, fobj):
        self.write_extern_end(fobj)

    def write_function_pre(self, fobj):
        self.write_shared(fobj)

    def write_function_post(self, fobj):
        self.write_shared_end(fobj)

    def write_extern(self, fobj):
        fobj.write('extern(System) {\n')

    def write_extern_end(self, fobj):
        fobj.write('}\n')

    def write_shared(self, fobj):
        fobj.write('__gshared {\n')

    def write_shared_end(self, fobj):
        fobj.write('}\n')

    def write_function_def(self, fobj, func):
        fobj.write('{} {}('.format(func.proto.ret.to_d(), func.proto.name))
        fobj.write(', '.join(param.type.to_d() for param in func.params))
        fobj.write(');\n')

    def write_function(self, fobj, func):
        fobj.write('fp_{0} {0};\n'.format(func.proto.name))

    def write_function_prototype(self, fobj, func):
        fobj.write('alias fp_{} = {} function('
                .format(func.proto.name, func.proto.ret.to_d()))
        fobj.write(', '.join(param.type.to_d() for param in func.params))
        fobj.write(');\n')

    def write_boolean(self, fobj, name, value=False):
        if value:
            fobj.write('bool {} = true;\n'.format(name))
        else:
            fobj.write('bool {};\n'.format(name))

    def write_enum(self, fobj, name, value, type='uint'):
        if isinstance(value, basestring) and '"' in value:
            type = 'const(char)*'

        fobj.write('enum {} {} = {};\n'.format(type, name, value))

    def write_opaque_struct(self, fobj, name):
        fobj.write('struct _{name}; alias {name} = _{name}*;\n'.format(name=name))

    def write_alias(self, fobj, newn, decl):
        fobj.write('alias {} = {};\n'.format(newn, decl))