Bunch of small tweaks to make this compile as C++ code without errors/warnings.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 15 Nov 2009 14:02:18 -0500
changeset 798 5dd67cc04cf9
parent 797 58d15db2881e
child 799 38370fa657fb
Bunch of small tweaks to make this compile as C++ code without errors/warnings.
mojoshader.c
mojoshader_assembler.c
mojoshader_lexer.re
mojoshader_opengl.c
mojoshader_preprocessor.c
--- a/mojoshader.c	Sun Nov 15 14:01:41 2009 -0500
+++ b/mojoshader.c	Sun Nov 15 14:02:18 2009 -0500
@@ -50,7 +50,7 @@
     RegisterType regtype;
     int regnum;
     MOJOSHADER_usage usage;
-    int index;
+    unsigned int index;
     int writemask;
     int misc;
     const VariableList *array;
@@ -283,7 +283,7 @@
 
 static inline void push_output(Context *ctx, OutputList *section)
 {
-    assert(ctx->output_stack_len < STATICARRAYLEN(ctx->output_stack));
+    assert(ctx->output_stack_len < (int) (STATICARRAYLEN(ctx->output_stack)));
     ctx->output_stack[ctx->output_stack_len] = ctx->output;
     ctx->indent_stack[ctx->output_stack_len] = ctx->indent;
     ctx->output_stack_len++;
@@ -938,7 +938,7 @@
     } // if
 
     char swizzle_str[6];
-    int i = 0;
+    size_t i = 0;
     const int scalar = scalar_register(ctx->shader_type, arg->regtype, arg->regnum);
     if (!scalar && !no_swizzle(arg->swizzle))
     {
@@ -995,7 +995,7 @@
     } // if
 
     char writemask_str[6];
-    int i = 0;
+    size_t i = 0;
     const int scalar = scalar_register(ctx->shader_type, arg->regtype, arg->regnum);
     if (!scalar && !writemask_xyzw(arg->writemask))
     {
@@ -1030,7 +1030,7 @@
 } // make_D3D_destarg_string
 
 
-static const char *make_D3D_srcarg_string(Context *ctx, const int idx)
+static const char *make_D3D_srcarg_string(Context *ctx, const size_t idx)
 {
     if (idx >= STATICARRAYLEN(ctx->source_args))
     {
@@ -1730,7 +1730,7 @@
     return get_GLSL_varname(ctx, arg->regtype, arg->regnum);
 } // get_GLSL_destarg_varname
 
-static const char *get_GLSL_srcarg_varname(Context *ctx, int idx)
+static const char *get_GLSL_srcarg_varname(Context *ctx, const size_t idx)
 {
     if (idx >= STATICARRAYLEN(ctx->source_args))
     {
@@ -1808,7 +1808,7 @@
                                                        arg->regnum, regnum_str,
                                                        sizeof (regnum_str));
     char writemask_str[6];
-    int i = 0;
+    size_t i = 0;
     const int scalar = scalar_register(ctx->shader_type, arg->regtype, arg->regnum);
     if (!scalar && !writemask_xyzw(arg->writemask))
     {
@@ -1862,8 +1862,8 @@
 } // make_GLSL_swizzle_string
 
 
-static char *make_GLSL_srcarg_string(Context *ctx, const int idx,
-                                     const int writemask)
+static const char *make_GLSL_srcarg_string(Context *ctx, const size_t idx,
+                                           const int writemask)
 {
     if (idx >= STATICARRAYLEN(ctx->source_args))
     {
@@ -2020,47 +2020,47 @@
     return retval;
 } // make_GLSL_srcarg_string
 
-static inline char *make_GLSL_srcarg_string_x(Context *ctx, const int idx)
+static inline const char *make_GLSL_srcarg_string_x(Context *ctx, const size_t idx)
 {
     return make_GLSL_srcarg_string(ctx, idx, (1 << 0));
 } // make_GLSL_srcarg_string_x
 
-static inline char *make_GLSL_srcarg_string_y(Context *ctx, const int idx)
+static inline const char *make_GLSL_srcarg_string_y(Context *ctx, const size_t idx)
 {
     return make_GLSL_srcarg_string(ctx, idx, (1 << 1));
 } // make_GLSL_srcarg_string_y
 
-static inline char *make_GLSL_srcarg_string_z(Context *ctx, const int idx)
+static inline const char *make_GLSL_srcarg_string_z(Context *ctx, const size_t idx)
 {
     return make_GLSL_srcarg_string(ctx, idx, (1 << 2));
 } // make_GLSL_srcarg_string_z
 
-static inline char *make_GLSL_srcarg_string_w(Context *ctx, const int idx)
+static inline const char *make_GLSL_srcarg_string_w(Context *ctx, const size_t idx)
 {
     return make_GLSL_srcarg_string(ctx, idx, (1 << 3));
 } // make_GLSL_srcarg_string_w
 
-static inline char *make_GLSL_srcarg_string_scalar(Context *ctx, const int idx)
+static inline const char *make_GLSL_srcarg_string_scalar(Context *ctx, const size_t idx)
 {
     return make_GLSL_srcarg_string_x(ctx, idx);
 } // make_GLSL_srcarg_string_scalar
 
-static inline char *make_GLSL_srcarg_string_full(Context *ctx, const int idx)
+static inline const char *make_GLSL_srcarg_string_full(Context *ctx, const size_t idx)
 {
     return make_GLSL_srcarg_string(ctx, idx, 0xF);
 } // make_GLSL_srcarg_string_full
 
-static inline char *make_GLSL_srcarg_string_masked(Context *ctx, const int idx)
+static inline const char *make_GLSL_srcarg_string_masked(Context *ctx, const size_t idx)
 {
     return make_GLSL_srcarg_string(ctx, idx, ctx->dest_arg.writemask);
 } // make_GLSL_srcarg_string_masked
 
-static inline char *make_GLSL_srcarg_string_vec3(Context *ctx, const int idx)
+static inline const char *make_GLSL_srcarg_string_vec3(Context *ctx, const size_t idx)
 {
     return make_GLSL_srcarg_string(ctx, idx, 0x7);
 } // make_GLSL_srcarg_string_vec3
 
-static inline char *make_GLSL_srcarg_string_vec2(Context *ctx, const int idx)
+static inline const char *make_GLSL_srcarg_string_vec2(Context *ctx, const size_t idx)
 {
     return make_GLSL_srcarg_string(ctx, idx, 0x3);
 } // make_GLSL_srcarg_string_vec2
@@ -3565,7 +3565,7 @@
     } // switch
 
     char swizzle_str[6];
-    int i = 0;
+    size_t i = 0;
 
     if (ctx->support_nv4)  // vFace must be output as "vFace.x" in nv4.
     {
@@ -3612,7 +3612,7 @@
     return get_ARB1_varname(ctx, arg->regtype, arg->regnum);
 } // get_ARB1_destarg_varname
 
-static const char *get_ARB1_srcarg_varname(Context *ctx, int idx)
+static const char *get_ARB1_srcarg_varname(Context *ctx, const size_t idx)
 {
     if (idx >= STATICARRAYLEN(ctx->source_args))
     {
@@ -3666,7 +3666,7 @@
     } // if
 
     char writemask_str[6];
-    int i = 0;
+    size_t i = 0;
     const int scalar = scalar_register(ctx->shader_type, arg->regtype, arg->regnum);
     if (!scalar && !writemask_xyzw(arg->writemask))
     {
@@ -3738,7 +3738,7 @@
 } // emit_ARB1_dest_modifiers
 
 
-static const char *make_ARB1_srcarg_string(Context *ctx, const int idx)
+static const char *make_ARB1_srcarg_string(Context *ctx, const size_t idx)
 {
     if (idx >= STATICARRAYLEN(ctx->source_args))
     {
@@ -4641,7 +4641,9 @@
         const char *topbranch = get_ARB1_branch_label_name(ctx, toplabel);
         const char *failbranch = get_ARB1_branch_label_name(ctx, faillabel);
 
-        assert(ctx->branch_labels_stack_index < STATICARRAYLEN(ctx->branch_labels_stack)-1);
+        assert(((size_t) ctx->branch_labels_stack_index) <
+                STATICARRAYLEN(ctx->branch_labels_stack)-1);
+
         ctx->branch_labels_stack[ctx->branch_labels_stack_index++] = toplabel;
         ctx->branch_labels_stack[ctx->branch_labels_stack_index++] = faillabel;
 
@@ -4701,7 +4703,9 @@
         const int label = allocate_branch_label(ctx);
         const char *failbranch = get_ARB1_branch_label_name(ctx, label);
 
-        assert(ctx->branch_labels_stack_index < STATICARRAYLEN(ctx->branch_labels_stack));
+        assert(((size_t) ctx->branch_labels_stack_index)
+                 < STATICARRAYLEN(ctx->branch_labels_stack));
+
         ctx->branch_labels_stack[ctx->branch_labels_stack_index++] = label;
 
         output_line(ctx, "BRA %s (EQ.x);", failbranch);
@@ -5368,10 +5372,10 @@
     if (reg == NULL)
         return swizzle;
 
-    int i;
-    const MOJOSHADER_swizzle *swiz = ctx->swizzles;
-    for (i = 0; i < ctx->swizzles_count; i++, swiz++)
-    {
+    size_t i;
+    for (i = 0; i < ctx->swizzles_count; i++)
+    {
+        const MOJOSHADER_swizzle *swiz = &ctx->swizzles[i];
         if ((swiz->usage == reg->usage) && (swiz->index == reg->index))
         {
             return ( (((int)(swiz->swizzles[((swizzle >> 0) & 0x3)])) << 0) |
@@ -6635,7 +6639,7 @@
     } // if
     else
     {
-        if (retval != (insttoks+1))
+        if (((uint32)retval) != (insttoks+1))
         {
             failf(ctx, "wrong token count (%u, not %u) for opcode '%s'.",
                     (uint) retval, (uint) (insttoks+1),
@@ -6849,7 +6853,7 @@
 
 static int find_profile_id(const char *profile)
 {
-    int i;
+    size_t i;
     for (i = 0; i < STATICARRAYLEN(profileMap); i++)
     {
         const char *name = profileMap[i].from;
@@ -7596,11 +7600,11 @@
 
 static void verify_swizzles(Context *ctx)
 {
-    int i;
+    size_t i;
     const char *failmsg = "invalid swizzle";
-    const MOJOSHADER_swizzle *swiz = ctx->swizzles;
-    for (i = 0; i < ctx->swizzles_count; i++, swiz++)
-    {
+    for (i = 0; i < ctx->swizzles_count; i++)
+    {
+        const MOJOSHADER_swizzle *swiz = &ctx->swizzles[i];
         if (swiz->swizzles[0] > 3) { fail(ctx, failmsg); return; }
         if (swiz->swizzles[1] > 3) { fail(ctx, failmsg); return; }
         if (swiz->swizzles[2] > 3) { fail(ctx, failmsg); return; }
--- a/mojoshader_assembler.c	Sun Nov 15 14:01:41 2009 -0500
+++ b/mojoshader_assembler.c	Sun Nov 15 14:02:18 2009 -0500
@@ -357,7 +357,7 @@
 
 static int ui32fromtoken(Context *ctx, uint32 *_val)
 {
-    int i;
+    unsigned int i;
     for (i = 0; i < ctx->tokenlen; i++)
     {
         if ((ctx->token[i] < '0') || (ctx->token[i] > '9'))
@@ -370,7 +370,7 @@
         return 0;
     } // if
 
-    const int len = i;
+    const unsigned int len = i;
     uint32 val = 0;
     uint32 mult = 1;
     while (i--)
@@ -540,6 +540,18 @@
 } // set_result_shift
 
 
+static inline int tokenbuf_overflow(Context *ctx)
+{
+    if ( ctx->tokenbufpos >= ((int) (STATICARRAYLEN(ctx->tokenbuf))) )
+    {
+        fail(ctx, "Too many tokens");
+        return 1;
+    } // if
+
+    return 0;
+} // tokenbuf_overflow
+
+
 static int parse_destination_token(Context *ctx)
 {
     DestArgInfo *info = &ctx->dest_arg;
@@ -641,11 +653,8 @@
 
     info->orig_writemask = info->writemask;
 
-    if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
-    {
-        fail(ctx, "Too many tokens");
+    if (tokenbuf_overflow(ctx))
         return 1;
-    } // if
 
     ctx->tokenbuf[ctx->tokenbufpos++] =
             ( ((((uint32) 1)) << 31) |
@@ -676,11 +685,8 @@
 {
     int retval = 1;
 
-    if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
-    {
-        fail(ctx, "Too many tokens");
+    if (tokenbuf_overflow(ctx))
         return 0;
-    } // if
 
     // mark this now, so optional relative addressing token is placed second.
     uint32 *outtoken = &ctx->tokenbuf[ctx->tokenbufpos++];
@@ -943,7 +949,7 @@
 
 static int parse_dcl_usage(Context *ctx, uint32 *val, int *issampler)
 {
-    int i;
+    size_t i;
     static const char *samplerusagestrs[] = { "_2d", "_cube", "_volume" };
     static const char *usagestrs[] = {
         "_position", "_blendweight", "_blendindices", "_normal", "_psize",
@@ -1147,7 +1153,7 @@
 static int parse_condition(Context *ctx, uint32 *controls)
 {
     static const char *comps[] = { "_gt", "_eq", "_ge", "_lt", "_ne", "_le" };
-    int i;
+    size_t i;
 
     if (ctx->tokenlen >= 3)
     {
@@ -1155,7 +1161,7 @@
         {
             if (check_token_segment(ctx, comps[i]))
             {
-                *controls = i + 1;
+                *controls = (uint32) (i + 1);
                 return 1;
             } // if
         } // for
@@ -1197,7 +1203,7 @@
 
     else  // find the instruction.
     {
-        int i;
+        size_t i;
         for (i = 0; i < STATICARRAYLEN(instructions); i++)
         {
             const char *opcode_string = instructions[i].opcode_string;
@@ -1558,18 +1564,18 @@
     const size_t extra = CTAB_SIZE + sizeof (uint32);
     if (len <= (ctx->ctab_len - extra))
     {
-        void *ptr = ctx->ctab + extra;
+        uint8 *ptr = ctx->ctab + extra;
         if (len == 0)
-            return ( (uint32) (((uint8 *) ptr) - ctx->ctab) ) - sizeof (uint32);
-        else if ((len == 1) && ((ptr = memchr(ptr, bytes[0], ctx->ctab_len - len)) != NULL))
-            return ( (uint32) (((uint8 *) ptr) - ctx->ctab) ) - sizeof (uint32);
+            return ( (uint32) (ptr - ctx->ctab) ) - sizeof (uint32);
+        else if ((len == 1) && ((ptr = (uint8 *) memchr(ptr, bytes[0], ctx->ctab_len - len)) != NULL))
+            return ( (uint32) (ptr - ctx->ctab) ) - sizeof (uint32);
         else  // search for the string of bytes...
         {
-            while ((ptr = memchr(ptr, bytes[0], ctx->ctab_len - len)) != NULL)
+            while ((ptr = (uint8 *) memchr(ptr, bytes[0], ctx->ctab_len - len)) != NULL)
             {
                 if (memcmp(ptr, bytes, len) == 0)  // this is it?
-                    return ( (uint32) (((uint8 *) ptr) - ctx->ctab) ) - sizeof (uint32);
-                ptr++;
+                    return ( (uint32) (ptr - ctx->ctab) ) - sizeof (uint32);
+                ptr++;  // !!! FIXME: should this be "ptr += len;"  ?
             } // while
         } // else
     } // if
@@ -1721,7 +1727,7 @@
     else
         output_comment_string(ctx, creator);
 
-    int i;
+    unsigned int i;
     for (i = 0; i < comment_count; i++)
         output_comment_string(ctx, comments[i]);
 
@@ -1791,7 +1797,7 @@
                 assert(retval != &MOJOSHADER_out_of_mem_data);
                 assert((error->error_position % sizeof (uint32)) == 0);
 
-                const int pos = error->error_position / sizeof (uint32);
+                const size_t pos = error->error_position / sizeof (uint32);
                 if (pos >= ctx->output_len)
                     error->error_position = -1;  // oh well.
                 else
--- a/mojoshader_lexer.re	Sun Nov 15 14:01:41 2009 -0500
+++ b/mojoshader_lexer.re	Sun Nov 15 14:02:18 2009 -0500
@@ -25,7 +25,7 @@
 typedef unsigned char uchar;
 
 /*!max:re2c */
-#define RET(t) do { return update_state(s, eoi, cursor, token, t); } while (0)
+#define RET(t) return update_state(s, eoi, cursor, token, (Token) t)
 #define YYCTYPE uchar
 #define YYCURSOR cursor
 #define YYLIMIT limit
--- a/mojoshader_opengl.c	Sun Nov 15 14:01:41 2009 -0500
+++ b/mojoshader_opengl.c	Sun Nov 15 14:02:18 2009 -0500
@@ -54,13 +54,13 @@
 {
     MOJOSHADER_shaderType shader_type;
     const MOJOSHADER_uniform *uniform;
-    GLuint location;
+    GLint location;
 } UniformMap;
 
 typedef struct
 {
     const MOJOSHADER_attribute *attribute;
-    GLuint location;
+    GLint location;
 } AttributeMap;
 
 struct MOJOSHADER_glProgram
@@ -132,7 +132,7 @@
     uint32 generation;
 
     // This tells us which vertex attribute arrays we have enabled.
-    int max_attrs;
+    GLint max_attrs;
     uint8 want_attr[32];
     uint8 have_attr[32];
 
@@ -436,7 +436,7 @@
                                         const GLfloat *f)
 {
     const GLint loc = ctx->glGetUniformLocation(program->handle, u->name);
-    if (loc != -1)   // not optimized out?
+    if (loc >= 0)   // not optimized out?
         ctx->glUniform4fv(loc, u->array_count, f);
 } // impl_GLSL_PushConstantArray
 
@@ -1034,7 +1034,7 @@
 
     if (ctx->have_base_opengl)
     {
-        int i;
+        size_t i;
         for (i = 0; i < STATICARRAYLEN(profile_priorities); i++)
         {
             const char *profile = profile_priorities[i];
@@ -1406,7 +1406,7 @@
     for (i = 0; i < pd->sampler_count; i++)
     {
         const GLint loc = ctx->profileGetSamplerLocation(program, shader, i);
-        if (loc != -1)  // maybe the Sampler was optimized out?
+        if (loc >= 0)  // maybe the Sampler was optimized out?
             ctx->profilePushSampler(loc, s[i].index);
     } // for
 } // lookup_samplers
@@ -1421,14 +1421,14 @@
     for (i = 0; i < pd->attribute_count; i++)
     {
         const GLint loc = ctx->profileGetAttribLocation(program, i);
-        if (loc != -1)  // maybe the Attribute was optimized out?
+        if (loc >= 0)  // maybe the Attribute was optimized out?
         {
             AttributeMap *map = &program->attributes[program->attribute_count];
             map->attribute = &a[i];
-            map->location = (GLuint) loc;
+            map->location = loc;
             program->attribute_count++;
 
-            if (loc > STATICARRAYLEN(ctx->want_attr))
+            if (((size_t)loc) > STATICARRAYLEN(ctx->want_attr))
             {
                 assert(0 && "Static array is too small.");  // laziness fail.
                 return 0;
@@ -1722,7 +1722,7 @@
     const GLenum gl_type = opengl_attr_type(type);
     const GLboolean norm = (normalized) ? GL_TRUE : GL_FALSE;
     const int count = ctx->bound_program->attribute_count;
-    GLuint gl_index = 0;
+    GLint gl_index = 0;
     int i;
 
     for (i = 0; i < count; i++)
@@ -1862,7 +1862,7 @@
             {
                 const size_t count = size;
                 const uint8 *b = &srcb[index];
-                int i;
+                size_t i;
                 for (i = 0; i < count; i++)
                     dstb[i] = (GLint) b[i];
                 dstb += count;
--- a/mojoshader_preprocessor.c	Sun Nov 15 14:01:41 2009 -0500
+++ b/mojoshader_preprocessor.c	Sun Nov 15 14:02:18 2009 -0500
@@ -306,7 +306,7 @@
 
 static char *flatten_buffer(Buffer *buffer, MOJOSHADER_malloc m, void *d)
 {
-    char *retval = m(buffer->total_bytes + 1, d);
+    char *retval = (char *) m(buffer->total_bytes + 1, d);
     if (retval == NULL)
         return NULL;
     BufferList *item = &buffer->head;
@@ -490,7 +490,7 @@
 
 static void put_all_defines(Context *ctx)
 {
-    int i;
+    size_t i;
     for (i = 0; i < STATICARRAYLEN(ctx->define_hashtable); i++)
     {
         Define *bucket = ctx->define_hashtable[i];
@@ -651,7 +651,7 @@
                             MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
 {
     int okay = 1;
-    int i = 0;
+    unsigned int i = 0;
 
     // the preprocessor is internal-only, so we verify all these are != NULL.
     assert(m != NULL);
@@ -946,6 +946,7 @@
 static void handle_pp_define(Context *ctx)
 {
     IncludeState *state = ctx->include_stack;
+    int done = 0;
 
     if (lexer(state) != TOKEN_IDENTIFIER)
     {
@@ -974,6 +975,9 @@
 
     int params = 0;
     char **idents = NULL;
+    MOJOSHADER_malloc m = ctx->malloc;
+    void *d = ctx->malloc_data;
+    static const char space = ' ';
 
     if (state->tokenval == ((Token) ' '))
         lexer(state);  // skip it.
@@ -1047,12 +1051,7 @@
     Buffer buffer;
     init_buffer(&buffer);
 
-    MOJOSHADER_malloc m = ctx->malloc;
-    void *d = ctx->malloc_data;
-    static const char space = ' ';
-
     state->report_whitespace = 1;
-    int done = 0;
     while ((!done) && (!ctx->out_of_memory))
     {
         const Token token = lexer(state);
@@ -1196,6 +1195,10 @@
         return 0;
     } // if
 
+    const char *fname = NULL;
+    const char *val = NULL;
+    size_t vallen = 0;
+
     IncludeState *state = ctx->include_stack;
     char *sym = (char *) alloca(state->tokenlen+1);
     memcpy(sym, state->token, state->tokenlen);
@@ -1311,9 +1314,9 @@
         } // if
     } // if
 
-    const char *val = def->definition;
-    const size_t vallen = strlen(val);
-    const char *fname = state->filename;
+    val = def->definition;
+    vallen = strlen(val);
+    fname = state->filename;
     if (!push_source(ctx, fname, val, vallen, state->line, NULL, params))
     {
         assert(ctx->out_of_memory);
@@ -1349,7 +1352,7 @@
         { 11, ((Token) '!') }, { 11, ((Token) '~') },
     };
 
-    int i;
+    size_t i;
     for (i = 0; i < STATICARRAYLEN(ops); i++)
     {
         if (ops[i].token == token)
@@ -1369,7 +1372,7 @@
 static long interpret_rpn(const RpnTokens *tokens, int tokencount, int *error)
 {
     long stack[128];
-    int stacksize = 0;
+    size_t stacksize = 0;
 
     *error = 1;
 
@@ -1449,8 +1452,8 @@
     RpnTokens output[128];
     Token stack[64];
     Token previous_token = TOKEN_UNKNOWN;
-    int outputsize = 0;
-    int stacksize = 0;
+    size_t outputsize = 0;
+    size_t stacksize = 0;
     int matched = 0;
     int done = 0;