github.com/acrespo/mobile@v0.0.0-20190107162257-dc0771356504/gl/gendebug.go (about)

     1  // Copyright 2014 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build ignore
     6  
     7  // The gendebug program takes gl.go and generates a version of it
     8  // where each function includes tracing code that writes its arguments
     9  // to the standard log.
    10  package main
    11  
    12  import (
    13  	"bytes"
    14  	"flag"
    15  	"fmt"
    16  	"go/ast"
    17  	"go/format"
    18  	"go/parser"
    19  	"go/printer"
    20  	"go/token"
    21  	"io/ioutil"
    22  	"log"
    23  	"os"
    24  	"strconv"
    25  )
    26  
    27  var enumWhitelist = []string{
    28  	"POINTS",
    29  	"LINES",
    30  	"LINE_LOOP",
    31  	"LINE_STRIP",
    32  	"TRIANGLES",
    33  	"TRIANGLE_STRIP",
    34  	"TRIANGLE_FAN",
    35  	"SRC_COLOR",
    36  	"ONE_MINUS_SRC_COLOR",
    37  	"SRC_ALPHA",
    38  	"ONE_MINUS_SRC_ALPHA",
    39  	"DST_ALPHA",
    40  	"ONE_MINUS_DST_ALPHA",
    41  	"DST_COLOR",
    42  	"ONE_MINUS_DST_COLOR",
    43  	"SRC_ALPHA_SATURATE",
    44  	"FUNC_ADD",
    45  	"BLEND_EQUATION",
    46  	"BLEND_EQUATION_RGB",
    47  	"BLEND_EQUATION_ALPHA",
    48  	"FUNC_SUBTRACT",
    49  	"FUNC_REVERSE_SUBTRACT",
    50  	"BLEND_DST_RGB",
    51  	"BLEND_SRC_RGB",
    52  	"BLEND_DST_ALPHA",
    53  	"BLEND_SRC_ALPHA",
    54  	"CONSTANT_COLOR",
    55  	"ONE_MINUS_CONSTANT_COLOR",
    56  	"CONSTANT_ALPHA",
    57  	"ONE_MINUS_CONSTANT_ALPHA",
    58  	"BLEND_COLOR",
    59  	"ARRAY_BUFFER",
    60  	"ELEMENT_ARRAY_BUFFER",
    61  	"ARRAY_BUFFER_BINDING",
    62  	"ELEMENT_ARRAY_BUFFER_BINDING",
    63  	"STREAM_DRAW",
    64  	"STATIC_DRAW",
    65  	"DYNAMIC_DRAW",
    66  	"BUFFER_SIZE",
    67  	"BUFFER_USAGE",
    68  	"CURRENT_VERTEX_ATTRIB",
    69  	"FRONT",
    70  	"BACK",
    71  	"FRONT_AND_BACK",
    72  	"TEXTURE_2D",
    73  	"CULL_FACE",
    74  	"BLEND",
    75  	"DITHER",
    76  	"STENCIL_TEST",
    77  	"DEPTH_TEST",
    78  	"SCISSOR_TEST",
    79  	"POLYGON_OFFSET_FILL",
    80  	"SAMPLE_ALPHA_TO_COVERAGE",
    81  	"SAMPLE_COVERAGE",
    82  	"INVALID_ENUM",
    83  	"INVALID_VALUE",
    84  	"INVALID_OPERATION",
    85  	"OUT_OF_MEMORY",
    86  	"CW",
    87  	"CCW",
    88  	"LINE_WIDTH",
    89  	"ALIASED_POINT_SIZE_RANGE",
    90  	"ALIASED_LINE_WIDTH_RANGE",
    91  	"CULL_FACE_MODE",
    92  	"FRONT_FACE",
    93  	"DEPTH_RANGE",
    94  	"DEPTH_WRITEMASK",
    95  	"DEPTH_CLEAR_VALUE",
    96  	"DEPTH_FUNC",
    97  	"STENCIL_CLEAR_VALUE",
    98  	"STENCIL_FUNC",
    99  	"STENCIL_FAIL",
   100  	"STENCIL_PASS_DEPTH_FAIL",
   101  	"STENCIL_PASS_DEPTH_PASS",
   102  	"STENCIL_REF",
   103  	"STENCIL_VALUE_MASK",
   104  	"STENCIL_WRITEMASK",
   105  	"STENCIL_BACK_FUNC",
   106  	"STENCIL_BACK_FAIL",
   107  	"STENCIL_BACK_PASS_DEPTH_FAIL",
   108  	"STENCIL_BACK_PASS_DEPTH_PASS",
   109  	"STENCIL_BACK_REF",
   110  	"STENCIL_BACK_VALUE_MASK",
   111  	"STENCIL_BACK_WRITEMASK",
   112  	"VIEWPORT",
   113  	"SCISSOR_BOX",
   114  	"COLOR_CLEAR_VALUE",
   115  	"COLOR_WRITEMASK",
   116  	"UNPACK_ALIGNMENT",
   117  	"PACK_ALIGNMENT",
   118  	"MAX_TEXTURE_SIZE",
   119  	"MAX_VIEWPORT_DIMS",
   120  	"SUBPIXEL_BITS",
   121  	"RED_BITS",
   122  	"GREEN_BITS",
   123  	"BLUE_BITS",
   124  	"ALPHA_BITS",
   125  	"DEPTH_BITS",
   126  	"STENCIL_BITS",
   127  	"POLYGON_OFFSET_UNITS",
   128  	"POLYGON_OFFSET_FACTOR",
   129  	"TEXTURE_BINDING_2D",
   130  	"SAMPLE_BUFFERS",
   131  	"SAMPLES",
   132  	"SAMPLE_COVERAGE_VALUE",
   133  	"SAMPLE_COVERAGE_INVERT",
   134  	"NUM_COMPRESSED_TEXTURE_FORMATS",
   135  	"COMPRESSED_TEXTURE_FORMATS",
   136  	"DONT_CARE",
   137  	"FASTEST",
   138  	"NICEST",
   139  	"GENERATE_MIPMAP_HINT",
   140  	"BYTE",
   141  	"UNSIGNED_BYTE",
   142  	"SHORT",
   143  	"UNSIGNED_SHORT",
   144  	"INT",
   145  	"UNSIGNED_INT",
   146  	"FLOAT",
   147  	"FIXED",
   148  	"DEPTH_COMPONENT",
   149  	"ALPHA",
   150  	"RGB",
   151  	"RGBA",
   152  	"LUMINANCE",
   153  	"LUMINANCE_ALPHA",
   154  	"UNSIGNED_SHORT_4_4_4_4",
   155  	"UNSIGNED_SHORT_5_5_5_1",
   156  	"UNSIGNED_SHORT_5_6_5",
   157  	"MAX_VERTEX_ATTRIBS",
   158  	"MAX_VERTEX_UNIFORM_VECTORS",
   159  	"MAX_VARYING_VECTORS",
   160  	"MAX_COMBINED_TEXTURE_IMAGE_UNITS",
   161  	"MAX_VERTEX_TEXTURE_IMAGE_UNITS",
   162  	"MAX_TEXTURE_IMAGE_UNITS",
   163  	"MAX_FRAGMENT_UNIFORM_VECTORS",
   164  	"SHADER_TYPE",
   165  	"DELETE_STATUS",
   166  	"LINK_STATUS",
   167  	"VALIDATE_STATUS",
   168  	"ATTACHED_SHADERS",
   169  	"ACTIVE_UNIFORMS",
   170  	"ACTIVE_UNIFORM_MAX_LENGTH",
   171  	"ACTIVE_ATTRIBUTES",
   172  	"ACTIVE_ATTRIBUTE_MAX_LENGTH",
   173  	"SHADING_LANGUAGE_VERSION",
   174  	"CURRENT_PROGRAM",
   175  	"NEVER",
   176  	"LESS",
   177  	"EQUAL",
   178  	"LEQUAL",
   179  	"GREATER",
   180  	"NOTEQUAL",
   181  	"GEQUAL",
   182  	"ALWAYS",
   183  	"KEEP",
   184  	"REPLACE",
   185  	"INCR",
   186  	"DECR",
   187  	"INVERT",
   188  	"INCR_WRAP",
   189  	"DECR_WRAP",
   190  	"VENDOR",
   191  	"RENDERER",
   192  	"VERSION",
   193  	"EXTENSIONS",
   194  	"NEAREST",
   195  	"LINEAR",
   196  	"NEAREST_MIPMAP_NEAREST",
   197  	"LINEAR_MIPMAP_NEAREST",
   198  	"NEAREST_MIPMAP_LINEAR",
   199  	"LINEAR_MIPMAP_LINEAR",
   200  	"TEXTURE_MAG_FILTER",
   201  	"TEXTURE_MIN_FILTER",
   202  	"TEXTURE_WRAP_S",
   203  	"TEXTURE_WRAP_T",
   204  	"TEXTURE",
   205  	"TEXTURE_CUBE_MAP",
   206  	"TEXTURE_BINDING_CUBE_MAP",
   207  	"TEXTURE_CUBE_MAP_POSITIVE_X",
   208  	"TEXTURE_CUBE_MAP_NEGATIVE_X",
   209  	"TEXTURE_CUBE_MAP_POSITIVE_Y",
   210  	"TEXTURE_CUBE_MAP_NEGATIVE_Y",
   211  	"TEXTURE_CUBE_MAP_POSITIVE_Z",
   212  	"TEXTURE_CUBE_MAP_NEGATIVE_Z",
   213  	"MAX_CUBE_MAP_TEXTURE_SIZE",
   214  	"TEXTURE0",
   215  	"TEXTURE1",
   216  	"TEXTURE2",
   217  	"TEXTURE3",
   218  	"TEXTURE4",
   219  	"TEXTURE5",
   220  	"TEXTURE6",
   221  	"TEXTURE7",
   222  	"TEXTURE8",
   223  	"TEXTURE9",
   224  	"TEXTURE10",
   225  	"TEXTURE11",
   226  	"TEXTURE12",
   227  	"TEXTURE13",
   228  	"TEXTURE14",
   229  	"TEXTURE15",
   230  	"TEXTURE16",
   231  	"TEXTURE17",
   232  	"TEXTURE18",
   233  	"TEXTURE19",
   234  	"TEXTURE20",
   235  	"TEXTURE21",
   236  	"TEXTURE22",
   237  	"TEXTURE23",
   238  	"TEXTURE24",
   239  	"TEXTURE25",
   240  	"TEXTURE26",
   241  	"TEXTURE27",
   242  	"TEXTURE28",
   243  	"TEXTURE29",
   244  	"TEXTURE30",
   245  	"TEXTURE31",
   246  	"ACTIVE_TEXTURE",
   247  	"REPEAT",
   248  	"CLAMP_TO_EDGE",
   249  	"MIRRORED_REPEAT",
   250  	"VERTEX_ATTRIB_ARRAY_ENABLED",
   251  	"VERTEX_ATTRIB_ARRAY_SIZE",
   252  	"VERTEX_ATTRIB_ARRAY_STRIDE",
   253  	"VERTEX_ATTRIB_ARRAY_TYPE",
   254  	"VERTEX_ATTRIB_ARRAY_NORMALIZED",
   255  	"VERTEX_ATTRIB_ARRAY_POINTER",
   256  	"VERTEX_ATTRIB_ARRAY_BUFFER_BINDING",
   257  	"IMPLEMENTATION_COLOR_READ_TYPE",
   258  	"IMPLEMENTATION_COLOR_READ_FORMAT",
   259  	"COMPILE_STATUS",
   260  	"INFO_LOG_LENGTH",
   261  	"SHADER_SOURCE_LENGTH",
   262  	"SHADER_COMPILER",
   263  	"SHADER_BINARY_FORMATS",
   264  	"NUM_SHADER_BINARY_FORMATS",
   265  	"LOW_FLOAT",
   266  	"MEDIUM_FLOAT",
   267  	"HIGH_FLOAT",
   268  	"LOW_INT",
   269  	"MEDIUM_INT",
   270  	"HIGH_INT",
   271  	"FRAMEBUFFER",
   272  	"RENDERBUFFER",
   273  	"RGBA4",
   274  	"RGB5_A1",
   275  	"RGB565",
   276  	"DEPTH_COMPONENT16",
   277  	"STENCIL_INDEX8",
   278  	"RENDERBUFFER_WIDTH",
   279  	"RENDERBUFFER_HEIGHT",
   280  	"RENDERBUFFER_INTERNAL_FORMAT",
   281  	"RENDERBUFFER_RED_SIZE",
   282  	"RENDERBUFFER_GREEN_SIZE",
   283  	"RENDERBUFFER_BLUE_SIZE",
   284  	"RENDERBUFFER_ALPHA_SIZE",
   285  	"RENDERBUFFER_DEPTH_SIZE",
   286  	"RENDERBUFFER_STENCIL_SIZE",
   287  	"FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE",
   288  	"FRAMEBUFFER_ATTACHMENT_OBJECT_NAME",
   289  	"FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL",
   290  	"FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE",
   291  	"COLOR_ATTACHMENT0",
   292  	"DEPTH_ATTACHMENT",
   293  	"STENCIL_ATTACHMENT",
   294  	"FRAMEBUFFER_COMPLETE",
   295  	"FRAMEBUFFER_INCOMPLETE_ATTACHMENT",
   296  	"FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT",
   297  	"FRAMEBUFFER_INCOMPLETE_DIMENSIONS",
   298  	"FRAMEBUFFER_UNSUPPORTED",
   299  	"FRAMEBUFFER_BINDING",
   300  	"RENDERBUFFER_BINDING",
   301  	"MAX_RENDERBUFFER_SIZE",
   302  	"INVALID_FRAMEBUFFER_OPERATION",
   303  	"DEPTH_BUFFER_BIT",
   304  	"STENCIL_BUFFER_BIT",
   305  	"COLOR_BUFFER_BIT",
   306  	"FLOAT_VEC2",
   307  	"FLOAT_VEC3",
   308  	"FLOAT_VEC4",
   309  	"INT_VEC2",
   310  	"INT_VEC3",
   311  	"INT_VEC4",
   312  	"BOOL",
   313  	"BOOL_VEC2",
   314  	"BOOL_VEC3",
   315  	"BOOL_VEC4",
   316  	"FLOAT_MAT2",
   317  	"FLOAT_MAT3",
   318  	"FLOAT_MAT4",
   319  	"SAMPLER_2D",
   320  	"SAMPLER_CUBE",
   321  	"FRAGMENT_SHADER",
   322  	"VERTEX_SHADER",
   323  	"FALSE",
   324  	"TRUE",
   325  	"ZERO",
   326  	"ONE",
   327  	"NO_ERROR",
   328  	"NONE",
   329  	"ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH",
   330  	"ACTIVE_UNIFORM_BLOCKS",
   331  	"ALREADY_SIGNALED",
   332  	"ANY_SAMPLES_PASSED",
   333  	"ANY_SAMPLES_PASSED_CONSERVATIVE",
   334  	"BLUE",
   335  	"BUFFER_ACCESS_FLAGS",
   336  	"BUFFER_MAP_LENGTH",
   337  	"BUFFER_MAP_OFFSET",
   338  	"BUFFER_MAPPED",
   339  	"BUFFER_MAP_POINTER",
   340  	"COLOR",
   341  	"COLOR_ATTACHMENT10",
   342  	"COLOR_ATTACHMENT1",
   343  	"COLOR_ATTACHMENT11",
   344  	"COLOR_ATTACHMENT12",
   345  	"COLOR_ATTACHMENT13",
   346  	"COLOR_ATTACHMENT14",
   347  	"COLOR_ATTACHMENT15",
   348  	"COLOR_ATTACHMENT2",
   349  	"COLOR_ATTACHMENT3",
   350  	"COLOR_ATTACHMENT4",
   351  	"COLOR_ATTACHMENT5",
   352  	"COLOR_ATTACHMENT6",
   353  	"COLOR_ATTACHMENT7",
   354  	"COLOR_ATTACHMENT8",
   355  	"COLOR_ATTACHMENT9",
   356  	"COMPARE_REF_TO_TEXTURE",
   357  	"COMPRESSED_R11_EAC",
   358  	"COMPRESSED_RG11_EAC",
   359  	"COMPRESSED_RGB8_ETC2",
   360  	"COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2",
   361  	"COMPRESSED_RGBA8_ETC2_EAC",
   362  	"COMPRESSED_SIGNED_R11_EAC",
   363  	"COMPRESSED_SIGNED_RG11_EAC",
   364  	"COMPRESSED_SRGB8_ALPHA8_ETC2_EAC",
   365  	"COMPRESSED_SRGB8_ETC2",
   366  	"COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2",
   367  	"CONDITION_SATISFIED",
   368  	"COPY_READ_BUFFER",
   369  	"COPY_READ_BUFFER_BINDING",
   370  	"COPY_WRITE_BUFFER",
   371  	"COPY_WRITE_BUFFER_BINDING",
   372  	"CURRENT_QUERY",
   373  	"DEPTH",
   374  	"DEPTH24_STENCIL8",
   375  	"DEPTH32F_STENCIL8",
   376  	"DEPTH_COMPONENT24",
   377  	"DEPTH_COMPONENT32F",
   378  	"DEPTH_STENCIL",
   379  	"DEPTH_STENCIL_ATTACHMENT",
   380  	"DRAW_BUFFER0",
   381  	"DRAW_BUFFER10",
   382  	"DRAW_BUFFER1",
   383  	"DRAW_BUFFER11",
   384  	"DRAW_BUFFER12",
   385  	"DRAW_BUFFER13",
   386  	"DRAW_BUFFER14",
   387  	"DRAW_BUFFER15",
   388  	"DRAW_BUFFER2",
   389  	"DRAW_BUFFER3",
   390  	"DRAW_BUFFER4",
   391  	"DRAW_BUFFER5",
   392  	"DRAW_BUFFER6",
   393  	"DRAW_BUFFER7",
   394  	"DRAW_BUFFER8",
   395  	"DRAW_BUFFER9",
   396  	"DRAW_FRAMEBUFFER",
   397  	"DRAW_FRAMEBUFFER_BINDING",
   398  	"DYNAMIC_COPY",
   399  	"DYNAMIC_READ",
   400  	"FLOAT_32_UNSIGNED_INT_24_8_REV",
   401  	"FLOAT_MAT2x3",
   402  	"FLOAT_MAT2x4",
   403  	"FLOAT_MAT3x2",
   404  	"FLOAT_MAT3x4",
   405  	"FLOAT_MAT4x2",
   406  	"FLOAT_MAT4x3",
   407  	"FRAGMENT_SHADER_DERIVATIVE_HINT",
   408  	"FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE",
   409  	"FRAMEBUFFER_ATTACHMENT_BLUE_SIZE",
   410  	"FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING",
   411  	"FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE",
   412  	"FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE",
   413  	"FRAMEBUFFER_ATTACHMENT_GREEN_SIZE",
   414  	"FRAMEBUFFER_ATTACHMENT_RED_SIZE",
   415  	"FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE",
   416  	"FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER",
   417  	"FRAMEBUFFER_DEFAULT",
   418  	"FRAMEBUFFER_INCOMPLETE_MULTISAMPLE",
   419  	"FRAMEBUFFER_UNDEFINED",
   420  	"GREEN",
   421  	"HALF_FLOAT",
   422  	"INT_2_10_10_10_REV",
   423  	"INTERLEAVED_ATTRIBS",
   424  	"INT_SAMPLER_2D",
   425  	"INT_SAMPLER_2D_ARRAY",
   426  	"INT_SAMPLER_3D",
   427  	"INT_SAMPLER_CUBE",
   428  	"INVALID_INDEX",
   429  	"MAJOR_VERSION",
   430  	"MAP_FLUSH_EXPLICIT_BIT",
   431  	"MAP_INVALIDATE_BUFFER_BIT",
   432  	"MAP_INVALIDATE_RANGE_BIT",
   433  	"MAP_READ_BIT",
   434  	"MAP_UNSYNCHRONIZED_BIT",
   435  	"MAP_WRITE_BIT",
   436  	"MAX",
   437  	"MAX_3D_TEXTURE_SIZE",
   438  	"MAX_ARRAY_TEXTURE_LAYERS",
   439  	"MAX_COLOR_ATTACHMENTS",
   440  	"MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS",
   441  	"MAX_COMBINED_UNIFORM_BLOCKS",
   442  	"MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS",
   443  	"MAX_DRAW_BUFFERS",
   444  	"MAX_ELEMENT_INDEX",
   445  	"MAX_ELEMENTS_INDICES",
   446  	"MAX_ELEMENTS_VERTICES",
   447  	"MAX_FRAGMENT_INPUT_COMPONENTS",
   448  	"MAX_FRAGMENT_UNIFORM_BLOCKS",
   449  	"MAX_FRAGMENT_UNIFORM_COMPONENTS",
   450  	"MAX_PROGRAM_TEXEL_OFFSET",
   451  	"MAX_SAMPLES",
   452  	"MAX_SERVER_WAIT_TIMEOUT",
   453  	"MAX_TEXTURE_LOD_BIAS",
   454  	"MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS",
   455  	"MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS",
   456  	"MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS",
   457  	"MAX_UNIFORM_BLOCK_SIZE",
   458  	"MAX_UNIFORM_BUFFER_BINDINGS",
   459  	"MAX_VARYING_COMPONENTS",
   460  	"MAX_VERTEX_OUTPUT_COMPONENTS",
   461  	"MAX_VERTEX_UNIFORM_BLOCKS",
   462  	"MAX_VERTEX_UNIFORM_COMPONENTS",
   463  	"MIN",
   464  	"MINOR_VERSION",
   465  	"MIN_PROGRAM_TEXEL_OFFSET",
   466  	"NUM_EXTENSIONS",
   467  	"NUM_PROGRAM_BINARY_FORMATS",
   468  	"NUM_SAMPLE_COUNTS",
   469  	"OBJECT_TYPE",
   470  	"PACK_ROW_LENGTH",
   471  	"PACK_SKIP_PIXELS",
   472  	"PACK_SKIP_ROWS",
   473  	"PIXEL_PACK_BUFFER",
   474  	"PIXEL_PACK_BUFFER_BINDING",
   475  	"PIXEL_UNPACK_BUFFER",
   476  	"PIXEL_UNPACK_BUFFER_BINDING",
   477  	"PRIMITIVE_RESTART_FIXED_INDEX",
   478  	"PROGRAM_BINARY_FORMATS",
   479  	"PROGRAM_BINARY_LENGTH",
   480  	"PROGRAM_BINARY_RETRIEVABLE_HINT",
   481  	"QUERY_RESULT",
   482  	"QUERY_RESULT_AVAILABLE",
   483  	"R11F_G11F_B10F",
   484  	"R16F",
   485  	"R16I",
   486  	"R16UI",
   487  	"R32F",
   488  	"R32I",
   489  	"R32UI",
   490  	"R8",
   491  	"R8I",
   492  	"R8_SNORM",
   493  	"R8UI",
   494  	"RASTERIZER_DISCARD",
   495  	"READ_BUFFER",
   496  	"READ_FRAMEBUFFER",
   497  	"READ_FRAMEBUFFER_BINDING",
   498  	"RED",
   499  	"RED_INTEGER",
   500  	"RENDERBUFFER_SAMPLES",
   501  	"RG",
   502  	"RG16F",
   503  	"RG16I",
   504  	"RG16UI",
   505  	"RG32F",
   506  	"RG32I",
   507  	"RG32UI",
   508  	"RG8",
   509  	"RG8I",
   510  	"RG8_SNORM",
   511  	"RG8UI",
   512  	"RGB10_A2",
   513  	"RGB10_A2UI",
   514  	"RGB16F",
   515  	"RGB16I",
   516  	"RGB16UI",
   517  	"RGB32F",
   518  	"RGB32I",
   519  	"RGB32UI",
   520  	"RGB8",
   521  	"RGB8I",
   522  	"RGB8_SNORM",
   523  	"RGB8UI",
   524  	"RGB9_E5",
   525  	"RGBA16F",
   526  	"RGBA16I",
   527  	"RGBA16UI",
   528  	"RGBA32F",
   529  	"RGBA32I",
   530  	"RGBA32UI",
   531  	"RGBA8",
   532  	"RGBA8I",
   533  	"RGBA8_SNORM",
   534  	"RGBA8UI",
   535  	"RGBA_INTEGER",
   536  	"RGB_INTEGER",
   537  	"RG_INTEGER",
   538  	"SAMPLER_2D_ARRAY",
   539  	"SAMPLER_2D_ARRAY_SHADOW",
   540  	"SAMPLER_2D_SHADOW",
   541  	"SAMPLER_3D",
   542  	"SAMPLER_BINDING",
   543  	"SAMPLER_CUBE_SHADOW",
   544  	"SEPARATE_ATTRIBS",
   545  	"SIGNALED",
   546  	"SIGNED_NORMALIZED",
   547  	"SRGB",
   548  	"SRGB8",
   549  	"SRGB8_ALPHA8",
   550  	"STATIC_COPY",
   551  	"STATIC_READ",
   552  	"STENCIL",
   553  	"STREAM_COPY",
   554  	"STREAM_READ",
   555  	"SYNC_CONDITION",
   556  	"SYNC_FENCE",
   557  	"SYNC_FLAGS",
   558  	"SYNC_FLUSH_COMMANDS_BIT",
   559  	"SYNC_GPU_COMMANDS_COMPLETE",
   560  	"SYNC_STATUS",
   561  	"TEXTURE_2D_ARRAY",
   562  	"TEXTURE_3D",
   563  	"TEXTURE_BASE_LEVEL",
   564  	"TEXTURE_BINDING_2D_ARRAY",
   565  	"TEXTURE_BINDING_3D",
   566  	"TEXTURE_COMPARE_FUNC",
   567  	"TEXTURE_COMPARE_MODE",
   568  	"TEXTURE_IMMUTABLE_FORMAT",
   569  	"TEXTURE_IMMUTABLE_LEVELS",
   570  	"TEXTURE_MAX_LEVEL",
   571  	"TEXTURE_MAX_LOD",
   572  	"TEXTURE_MIN_LOD",
   573  	"TEXTURE_SWIZZLE_A",
   574  	"TEXTURE_SWIZZLE_B",
   575  	"TEXTURE_SWIZZLE_G",
   576  	"TEXTURE_SWIZZLE_R",
   577  	"TEXTURE_WRAP_R",
   578  	"TIMEOUT_EXPIRED",
   579  	"TRANSFORM_FEEDBACK",
   580  	"TRANSFORM_FEEDBACK_ACTIVE",
   581  	"TRANSFORM_FEEDBACK_BINDING",
   582  	"TRANSFORM_FEEDBACK_BUFFER",
   583  	"TRANSFORM_FEEDBACK_BUFFER_BINDING",
   584  	"TRANSFORM_FEEDBACK_BUFFER_MODE",
   585  	"TRANSFORM_FEEDBACK_BUFFER_SIZE",
   586  	"TRANSFORM_FEEDBACK_BUFFER_START",
   587  	"TRANSFORM_FEEDBACK_PAUSED",
   588  	"TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN",
   589  	"TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH",
   590  	"TRANSFORM_FEEDBACK_VARYINGS",
   591  	"UNIFORM_ARRAY_STRIDE",
   592  	"UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES",
   593  	"UNIFORM_BLOCK_ACTIVE_UNIFORMS",
   594  	"UNIFORM_BLOCK_BINDING",
   595  	"UNIFORM_BLOCK_DATA_SIZE",
   596  	"UNIFORM_BLOCK_INDEX",
   597  	"UNIFORM_BLOCK_NAME_LENGTH",
   598  	"UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER",
   599  	"UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER",
   600  	"UNIFORM_BUFFER",
   601  	"UNIFORM_BUFFER_BINDING",
   602  	"UNIFORM_BUFFER_OFFSET_ALIGNMENT",
   603  	"UNIFORM_BUFFER_SIZE",
   604  	"UNIFORM_BUFFER_START",
   605  	"UNIFORM_IS_ROW_MAJOR",
   606  	"UNIFORM_MATRIX_STRIDE",
   607  	"UNIFORM_NAME_LENGTH",
   608  	"UNIFORM_OFFSET",
   609  	"UNIFORM_SIZE",
   610  	"UNIFORM_TYPE",
   611  	"UNPACK_IMAGE_HEIGHT",
   612  	"UNPACK_ROW_LENGTH",
   613  	"UNPACK_SKIP_IMAGES",
   614  	"UNPACK_SKIP_PIXELS",
   615  	"UNPACK_SKIP_ROWS",
   616  	"UNSIGNALED",
   617  	"UNSIGNED_INT_10F_11F_11F_REV",
   618  	"UNSIGNED_INT_2_10_10_10_REV",
   619  	"UNSIGNED_INT_24_8",
   620  	"UNSIGNED_INT_5_9_9_9_REV",
   621  	"UNSIGNED_INT_SAMPLER_2D",
   622  	"UNSIGNED_INT_SAMPLER_2D_ARRAY",
   623  	"UNSIGNED_INT_SAMPLER_3D",
   624  	"UNSIGNED_INT_SAMPLER_CUBE",
   625  	"UNSIGNED_INT_VEC2",
   626  	"UNSIGNED_INT_VEC3",
   627  	"UNSIGNED_INT_VEC4",
   628  	"UNSIGNED_NORMALIZED",
   629  	"VERTEX_ARRAY_BINDING",
   630  	"VERTEX_ATTRIB_ARRAY_DIVISOR",
   631  	"VERTEX_ATTRIB_ARRAY_INTEGER",
   632  	"WAIT_FAILED",
   633  }
   634  
   635  var outfile = flag.String("o", "", "result will be written to the file instead of stdout.")
   636  
   637  var fset = new(token.FileSet)
   638  
   639  func typeString(t ast.Expr) string {
   640  	buf := new(bytes.Buffer)
   641  	printer.Fprint(buf, fset, t)
   642  	return buf.String()
   643  }
   644  
   645  func typePrinter(t string) string {
   646  	switch t {
   647  	case "[]float32", "[]byte":
   648  		return "len(%d)"
   649  	}
   650  	return "%v"
   651  }
   652  
   653  func typePrinterArg(t, name string) string {
   654  	switch t {
   655  	case "[]float32", "[]byte":
   656  		return "len(" + name + ")"
   657  	}
   658  	return name
   659  }
   660  
   661  func die(err error) {
   662  	fmt.Fprintf(os.Stderr, err.Error())
   663  	os.Exit(1)
   664  }
   665  
   666  func main() {
   667  	flag.Parse()
   668  
   669  	f, err := parser.ParseFile(fset, "consts.go", nil, parser.ParseComments)
   670  	if err != nil {
   671  		die(err)
   672  	}
   673  	entries := enum(f)
   674  
   675  	f, err = parser.ParseFile(fset, "gl.go", nil, parser.ParseComments)
   676  	if err != nil {
   677  		die(err)
   678  	}
   679  
   680  	buf := new(bytes.Buffer)
   681  
   682  	fmt.Fprint(buf, preamble)
   683  
   684  	fmt.Fprintf(buf, "func (v Enum) String() string {\n")
   685  	fmt.Fprintf(buf, "\tswitch v {\n")
   686  	for _, e := range dedup(entries) {
   687  		fmt.Fprintf(buf, "\tcase 0x%x: return %q\n", e.value, e.name)
   688  	}
   689  	fmt.Fprintf(buf, "\t%s\n", `default: return fmt.Sprintf("gl.Enum(0x%x)", uint32(v))`)
   690  	fmt.Fprintf(buf, "\t}\n")
   691  	fmt.Fprintf(buf, "}\n\n")
   692  
   693  	for _, d := range f.Decls {
   694  		// Before:
   695  		// func (ctx *context) StencilMask(mask uint32) {
   696  		//	C.glStencilMask(C.GLuint(mask))
   697  		// }
   698  		//
   699  		// After:
   700  		// func (ctx *context) StencilMask(mask uint32) {
   701  		// 	defer func() {
   702  		// 		errstr := ctx.errDrain()
   703  		// 		log.Printf("gl.StencilMask(%v) %v", mask, errstr)
   704  		//	}()
   705  		//	C.glStencilMask(C.GLuint(mask))
   706  		// }
   707  		fn, ok := d.(*ast.FuncDecl)
   708  		if !ok {
   709  			continue
   710  		}
   711  		if fn.Recv == nil || fn.Recv.List[0].Names[0].Name != "ctx" {
   712  			continue
   713  		}
   714  		tname := "<unknown>"
   715  		t := fn.Recv.List[0].Type
   716  		if star, ok := t.(*ast.StarExpr); ok {
   717  			tname = "*" + star.X.(*ast.Ident).Name
   718  		} else if t, ok := t.(*ast.Ident); ok {
   719  			tname = t.Name
   720  		}
   721  
   722  		var (
   723  			params      []string
   724  			paramTypes  []string
   725  			results     []string
   726  			resultTypes []string
   727  		)
   728  
   729  		// Print function signature.
   730  		fmt.Fprintf(buf, "func (ctx %s) %s(", tname, fn.Name.Name)
   731  		for i, p := range fn.Type.Params.List {
   732  			if i > 0 {
   733  				fmt.Fprint(buf, ", ")
   734  			}
   735  			ty := typeString(p.Type)
   736  			for i, n := range p.Names {
   737  				if i > 0 {
   738  					fmt.Fprint(buf, ", ")
   739  				}
   740  				fmt.Fprintf(buf, "%s ", n.Name)
   741  				params = append(params, n.Name)
   742  				paramTypes = append(paramTypes, ty)
   743  			}
   744  			fmt.Fprint(buf, ty)
   745  		}
   746  		fmt.Fprintf(buf, ") (")
   747  		if fn.Type.Results != nil {
   748  			for i, r := range fn.Type.Results.List {
   749  				if i > 0 {
   750  					fmt.Fprint(buf, ", ")
   751  				}
   752  				ty := typeString(r.Type)
   753  				if len(r.Names) == 0 {
   754  					name := fmt.Sprintf("r%d", i)
   755  					fmt.Fprintf(buf, "%s ", name)
   756  					results = append(results, name)
   757  					resultTypes = append(resultTypes, ty)
   758  				}
   759  				for i, n := range r.Names {
   760  					if i > 0 {
   761  						fmt.Fprint(buf, ", ")
   762  					}
   763  					fmt.Fprintf(buf, "%s ", n.Name)
   764  					results = append(results, n.Name)
   765  					resultTypes = append(resultTypes, ty)
   766  				}
   767  				fmt.Fprint(buf, ty)
   768  			}
   769  		}
   770  		fmt.Fprintf(buf, ") {\n")
   771  
   772  		// gl.GetError is used by errDrain, which will be made part of
   773  		// all functions. So do not apply it to gl.GetError to avoid
   774  		// infinite recursion.
   775  		skip := fn.Name.Name == "GetError"
   776  
   777  		if !skip {
   778  			// Insert a defer block for tracing.
   779  			fmt.Fprintf(buf, "defer func() {\n")
   780  			fmt.Fprintf(buf, "\terrstr := ctx.errDrain()\n")
   781  			switch fn.Name.Name {
   782  			case "GetUniformLocation", "GetAttribLocation":
   783  				fmt.Fprintf(buf, "\tr0.name = name\n")
   784  			}
   785  			fmt.Fprintf(buf, "\tlog.Printf(\"gl.%s(", fn.Name.Name)
   786  			for i, p := range paramTypes {
   787  				if i > 0 {
   788  					fmt.Fprint(buf, ", ")
   789  				}
   790  				fmt.Fprint(buf, typePrinter(p))
   791  			}
   792  			fmt.Fprintf(buf, ") ")
   793  			if len(resultTypes) > 1 {
   794  				fmt.Fprint(buf, "(")
   795  			}
   796  			for i, r := range resultTypes {
   797  				if i > 0 {
   798  					fmt.Fprint(buf, ", ")
   799  				}
   800  				fmt.Fprint(buf, typePrinter(r))
   801  			}
   802  			if len(resultTypes) > 1 {
   803  				fmt.Fprint(buf, ") ")
   804  			}
   805  			fmt.Fprintf(buf, "%%v\"")
   806  			for i, p := range paramTypes {
   807  				fmt.Fprintf(buf, ", %s", typePrinterArg(p, params[i]))
   808  			}
   809  			for i, r := range resultTypes {
   810  				fmt.Fprintf(buf, ", %s", typePrinterArg(r, results[i]))
   811  			}
   812  			fmt.Fprintf(buf, ", errstr)\n")
   813  			fmt.Fprintf(buf, "}()\n")
   814  		}
   815  
   816  		// Print original body of function.
   817  		for _, s := range fn.Body.List {
   818  			if c := enqueueCall(s); c != nil {
   819  				c.Fun.(*ast.SelectorExpr).Sel.Name = "enqueueDebug"
   820  				setEnqueueBlocking(c)
   821  			}
   822  			printer.Fprint(buf, fset, s)
   823  			fmt.Fprintf(buf, "\n")
   824  		}
   825  		fmt.Fprintf(buf, "}\n\n")
   826  	}
   827  
   828  	b, err := format.Source(buf.Bytes())
   829  	if err != nil {
   830  		os.Stdout.Write(buf.Bytes())
   831  		die(err)
   832  	}
   833  
   834  	if *outfile == "" {
   835  		os.Stdout.Write(b)
   836  		return
   837  	}
   838  	if err := ioutil.WriteFile(*outfile, b, 0666); err != nil {
   839  		die(err)
   840  	}
   841  }
   842  
   843  func enqueueCall(stmt ast.Stmt) *ast.CallExpr {
   844  	exprStmt, ok := stmt.(*ast.ExprStmt)
   845  	if !ok {
   846  		return nil
   847  	}
   848  	call, ok := exprStmt.X.(*ast.CallExpr)
   849  	if !ok {
   850  		return nil
   851  	}
   852  	fun, ok := call.Fun.(*ast.SelectorExpr)
   853  	if !ok {
   854  		return nil
   855  	}
   856  	if fun.Sel.Name != "enqueue" {
   857  		return nil
   858  	}
   859  	return call
   860  }
   861  
   862  func setEnqueueBlocking(c *ast.CallExpr) {
   863  	lit := c.Args[0].(*ast.CompositeLit)
   864  	for _, elt := range lit.Elts {
   865  		kv := elt.(*ast.KeyValueExpr)
   866  		if kv.Key.(*ast.Ident).Name == "blocking" {
   867  			kv.Value = &ast.Ident{Name: "true"}
   868  			return
   869  		}
   870  	}
   871  	lit.Elts = append(lit.Elts, &ast.KeyValueExpr{
   872  		Key: &ast.Ident{
   873  			NamePos: lit.Rbrace,
   874  			Name:    "blocking",
   875  		},
   876  		Value: &ast.Ident{Name: "true"},
   877  	})
   878  }
   879  
   880  const preamble = `// Copyright 2014 The Go Authors.  All rights reserved.
   881  // Use of this source code is governed by a BSD-style
   882  // license that can be found in the LICENSE file.
   883  
   884  // Code generated from gl.go using go generate. DO NOT EDIT.
   885  // See doc.go for details.
   886  
   887  // +build linux darwin windows openbsd
   888  // +build gldebug
   889  
   890  package gl
   891  
   892  import (
   893  	"fmt"
   894  	"log"
   895  	"math"
   896  	"sync/atomic"
   897  	"unsafe"
   898  )
   899  
   900  func (ctx *context) errDrain() string {
   901  	var errs []Enum
   902  	for {
   903  		e := ctx.GetError()
   904  		if e == 0 {
   905  			break
   906  		}
   907  		errs = append(errs, e)
   908  	}
   909  	if len(errs) > 0 {
   910  		return fmt.Sprintf(" error: %v", errs)
   911  	}
   912  	return ""
   913  }
   914  
   915  func (ctx *context) enqueueDebug(c call) uintptr {
   916  	numCalls := atomic.AddInt32(&ctx.debug, 1)
   917  	if numCalls > 1 {
   918  		panic("concurrent calls made to the same GL context")
   919  	}
   920  	defer func() {
   921  		if atomic.AddInt32(&ctx.debug, -1) > 0 {
   922  			select {} // block so you see us in the panic
   923  		}
   924  	}()
   925  
   926  	return ctx.enqueue(c)
   927  }
   928  
   929  `
   930  
   931  type entry struct {
   932  	name  string
   933  	value uint64
   934  }
   935  
   936  func genWhitelistMap(whitelist []string) map[string]bool {
   937  	m := make(map[string]bool)
   938  	for _, v := range enumWhitelist {
   939  		m[v] = true
   940  	}
   941  	return m
   942  }
   943  
   944  // enum builds a list of all GL constants that make up the gl.Enum type.
   945  func enum(f *ast.File) []entry {
   946  	var entries []entry
   947  	whitelist := genWhitelistMap(enumWhitelist)
   948  	for _, d := range f.Decls {
   949  		gendecl, ok := d.(*ast.GenDecl)
   950  		if !ok {
   951  			continue
   952  		}
   953  		if gendecl.Tok != token.CONST {
   954  			continue
   955  		}
   956  		for _, s := range gendecl.Specs {
   957  			v, ok := s.(*ast.ValueSpec)
   958  			if !ok {
   959  				continue
   960  			}
   961  			if len(v.Names) != 1 || len(v.Values) != 1 {
   962  				continue
   963  			}
   964  			if _, ok := whitelist[v.Names[0].Name]; !ok {
   965  				continue
   966  			}
   967  			val, err := strconv.ParseUint(v.Values[0].(*ast.BasicLit).Value, 0, 64)
   968  			if err != nil {
   969  				log.Fatalf("enum %s: %v", v.Names[0].Name, err)
   970  			}
   971  			entries = append(entries, entry{v.Names[0].Name, val})
   972  		}
   973  	}
   974  	return entries
   975  }
   976  
   977  func dedup(entries []entry) []entry {
   978  	// Find all duplicates. Use "%d" as the name of any value with duplicates.
   979  	seen := make(map[uint64]int)
   980  	for _, e := range entries {
   981  		seen[e.value]++
   982  	}
   983  	var dedup []entry
   984  	for _, e := range entries {
   985  		switch seen[e.value] {
   986  		case 0: // skip, already here
   987  		case 1:
   988  			dedup = append(dedup, e)
   989  		default:
   990  			// value is duplicated
   991  			dedup = append(dedup, entry{fmt.Sprintf("%d", e.value), e.value})
   992  			seen[e.value] = 0
   993  		}
   994  	}
   995  	return dedup
   996  }