glcpp: Flag invalid pastes for integer followed by non-digits
[mesa.git] / src / glsl / glcpp / glcpp-parse.y
index 940830416c0ad32bd9d5596123c468996aca8176..8f17d0d8ab3196c471238dae79ad026c001e9520 100644 (file)
@@ -85,7 +85,6 @@ _token_create_ival (void *ctx, int type, int ival);
 static token_list_t *
 _token_list_create (void *ctx);
 
-/* Note: This function calls ralloc_steal on token. */
 static void
 _token_list_append (token_list_t *list, token_t *token);
 
@@ -106,9 +105,15 @@ _parser_active_list_pop (glcpp_parser_t *parser);
 static int
 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier);
 
+/* Expand list, and begin lexing from the result (after first
+ * prefixing a token of type 'head_token_type').
+ */
 static void
-_glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list);
+_glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
+                                  int head_token_type,
+                                  token_list_t *list);
 
+/* Perform macro expansion in-place on the given list. */
 static void
 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
                                 token_list_t *list);
@@ -128,8 +133,6 @@ _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
 static void
 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
 
-#define yylex glcpp_parser_lex
-
 static int
 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
 
@@ -157,10 +160,10 @@ add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
 %lex-param {glcpp_parser_t *parser}
 
 %expect 0
-%token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_UNDEF HASH_VERSION IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING NEWLINE OTHER PLACEHOLDER SPACE
+%token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE FUNC_IDENTIFIER OBJ_IDENTIFIER HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_LINE HASH_UNDEF HASH_VERSION IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING LINE_EXPANDED NEWLINE OTHER PLACEHOLDER SPACE
 %token PASTE
 %type <ival> expression INTEGER operator SPACE integer_constant
-%type <str> IDENTIFIER INTEGER_STRING OTHER
+%type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER
 %type <string_list> identifier_list
 %type <token> preprocessing_token conditional_token
 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
@@ -185,11 +188,19 @@ input:
 
 line:
        control_line {
-               ralloc_strcat (&parser->output, "\n");
+               ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
+       }
+|      HASH_LINE pp_tokens NEWLINE {
+               if (parser->skip_stack == NULL ||
+                   parser->skip_stack->type == SKIP_NO_SKIP)
+               {
+                       _glcpp_parser_expand_and_lex_from (parser,
+                                                          LINE_EXPANDED, $2);
+               }
        }
 |      text_line {
                _glcpp_parser_print_expanded_token_list (parser, $1);
-               ralloc_strcat (&parser->output, "\n");
+               ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
                ralloc_free ($1);
        }
 |      expanded_line
@@ -203,16 +214,34 @@ expanded_line:
 |      ELIF_EXPANDED expression NEWLINE {
                _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
        }
+|      LINE_EXPANDED integer_constant NEWLINE {
+               parser->has_new_line_number = 1;
+               parser->new_line_number = $2;
+               ralloc_asprintf_rewrite_tail (&parser->output,
+                                             &parser->output_length,
+                                             "#line %" PRIiMAX "\n",
+                                             $2);
+       }
+|      LINE_EXPANDED integer_constant integer_constant NEWLINE {
+               parser->has_new_line_number = 1;
+               parser->new_line_number = $2;
+               parser->has_new_source_number = 1;
+               parser->new_source_number = $3;
+               ralloc_asprintf_rewrite_tail (&parser->output,
+                                             &parser->output_length,
+                                             "#line %" PRIiMAX " %" PRIiMAX "\n",
+                                             $2, $3);
+       }
 ;
 
 control_line:
-       HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
+       HASH_DEFINE OBJ_IDENTIFIER replacement_list NEWLINE {
                _define_object_macro (parser, & @2, $2, $3);
        }
-|      HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
+|      HASH_DEFINE FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
                _define_function_macro (parser, & @2, $2, NULL, $5);
        }
-|      HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
+|      HASH_DEFINE FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
                _define_function_macro (parser, & @2, $2, $4, $6);
        }
 |      HASH_UNDEF IDENTIFIER NEWLINE {
@@ -234,7 +263,8 @@ control_line:
                if (parser->skip_stack == NULL ||
                    parser->skip_stack->type == SKIP_NO_SKIP)
                {
-                       _glcpp_parser_expand_if (parser, IF_EXPANDED, $2);
+                       _glcpp_parser_expand_and_lex_from (parser,
+                                                          IF_EXPANDED, $2);
                }       
                else
                {
@@ -273,7 +303,8 @@ control_line:
                if (parser->skip_stack &&
                    parser->skip_stack->type == SKIP_TO_ELSE)
                {
-                       _glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2);
+                       _glcpp_parser_expand_and_lex_from (parser,
+                                                          ELIF_EXPANDED, $2);
                }
                else
                {
@@ -296,12 +327,12 @@ control_line:
                        glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
                }
        }
-|      HASH_ELSE NEWLINE {
+|      HASH_ELSE {
                _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
-       }
-|      HASH_ENDIF NEWLINE {
+       } NEWLINE
+|      HASH_ENDIF {
                _glcpp_parser_skip_stack_pop (parser, & @1);
-       }
+       } NEWLINE
 |      HASH_VERSION integer_constant NEWLINE {
                macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
                if (macro) {
@@ -321,7 +352,7 @@ control_line:
                if ($2 >= 130 || $2 == 100)
                        add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
 
-               ralloc_asprintf_append (&parser->output, "#version %" PRIiMAX, $2);
+               ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "#version %" PRIiMAX, $2);
        }
 |      HASH NEWLINE
 ;
@@ -342,6 +373,9 @@ integer_constant:
 
 expression:
        integer_constant
+|      IDENTIFIER {
+               $$ = 0;
+       }
 |      expression OR expression {
                $$ = $1 || $3;
        }
@@ -763,8 +797,6 @@ _token_list_append (token_list_t *list, token_t *token)
        node->token = token;
        node->next = NULL;
 
-       ralloc_steal (list, token);
-
        if (list->head == NULL) {
                list->head = node;
        } else {
@@ -906,54 +938,54 @@ _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
 }
 
 static void
-_token_print (char **out, token_t *token)
+_token_print (char **out, size_t *len, token_t *token)
 {
        if (token->type < 256) {
-               ralloc_asprintf_append (out, "%c", token->type);
+               ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
                return;
        }
 
        switch (token->type) {
        case INTEGER:
-               ralloc_asprintf_append (out, "%" PRIiMAX, token->value.ival);
+               ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
                break;
        case IDENTIFIER:
        case INTEGER_STRING:
        case OTHER:
-               ralloc_strcat (out, token->value.str);
+               ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
                break;
        case SPACE:
-               ralloc_strcat (out, " ");
+               ralloc_asprintf_rewrite_tail (out, len, " ");
                break;
        case LEFT_SHIFT:
-               ralloc_strcat (out, "<<");
+               ralloc_asprintf_rewrite_tail (out, len, "<<");
                break;
        case RIGHT_SHIFT:
-               ralloc_strcat (out, ">>");
+               ralloc_asprintf_rewrite_tail (out, len, ">>");
                break;
        case LESS_OR_EQUAL:
-               ralloc_strcat (out, "<=");
+               ralloc_asprintf_rewrite_tail (out, len, "<=");
                break;
        case GREATER_OR_EQUAL:
-               ralloc_strcat (out, ">=");
+               ralloc_asprintf_rewrite_tail (out, len, ">=");
                break;
        case EQUAL:
-               ralloc_strcat (out, "==");
+               ralloc_asprintf_rewrite_tail (out, len, "==");
                break;
        case NOT_EQUAL:
-               ralloc_strcat (out, "!=");
+               ralloc_asprintf_rewrite_tail (out, len, "!=");
                break;
        case AND:
-               ralloc_strcat (out, "&&");
+               ralloc_asprintf_rewrite_tail (out, len, "&&");
                break;
        case OR:
-               ralloc_strcat (out, "||");
+               ralloc_asprintf_rewrite_tail (out, len, "||");
                break;
        case PASTE:
-               ralloc_strcat (out, "##");
+               ralloc_asprintf_rewrite_tail (out, len, "##");
                break;
        case COMMA_FINAL:
-               ralloc_strcat (out, ",");
+               ralloc_asprintf_rewrite_tail (out, len, ",");
                break;
        case PLACEHOLDER:
                /* Nothing to print. */
@@ -1025,16 +1057,23 @@ _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
        /* Two string-valued tokens can usually just be mashed
         * together.
         *
-        * XXX: This isn't actually legitimate. Several things here
-        * should result in a diagnostic since the result cannot be a
-        * valid, single pre-processing token. For example, pasting
-        * "123" and "abc" is not legal, but we don't catch that
-        * here. */
+        * There are some exceptions here. Notably, if the first token
+        * is a string representing an integer, then the second token
+        * must also be a an integer and must begin with a digit.
+        */
        if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
            (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
        {
                char *str;
 
+               if (token->type == INTEGER_STRING) {
+                       if (other->type != INTEGER_STRING)
+                               goto FAIL;
+                       if (other->value.str[0] < '0' ||
+                           other->value.str[0] > '9')
+                               goto FAIL;
+               }
+
                str = ralloc_asprintf (token, "%s%s", token->value.str,
                                       other->value.str);
                combined = _token_create_str (token, token->type, str);
@@ -1042,12 +1081,13 @@ _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
                return combined;
        }
 
+    FAIL:
        glcpp_error (&token->location, parser, "");
-       ralloc_strcat (&parser->info_log, "Pasting \"");
-       _token_print (&parser->info_log, token);
-       ralloc_strcat (&parser->info_log, "\" and \"");
-       _token_print (&parser->info_log, other);
-       ralloc_strcat (&parser->info_log, "\" does not give a valid preprocessing token.\n");
+       ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
+       _token_print (&parser->info_log, &parser->info_log_length, token);
+       ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
+       _token_print (&parser->info_log, &parser->info_log_length, other);
+       ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
 
        return token;
 }
@@ -1061,7 +1101,7 @@ _token_list_print (glcpp_parser_t *parser, token_list_t *list)
                return;
 
        for (node = list->head; node; node = node->next)
-               _token_print (&parser->output, node->token);
+               _token_print (&parser->output, &parser->output_length, node->token);
 }
 
 void
@@ -1107,34 +1147,59 @@ glcpp_parser_create (const struct gl_extensions *extensions, int api)
        parser->lex_from_node = NULL;
 
        parser->output = ralloc_strdup(parser, "");
+       parser->output_length = 0;
        parser->info_log = ralloc_strdup(parser, "");
+       parser->info_log_length = 0;
        parser->error = 0;
 
+       parser->has_new_line_number = 0;
+       parser->new_line_number = 1;
+       parser->has_new_source_number = 0;
+       parser->new_source_number = 0;
+
        /* Add pre-defined macros. */
-       add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
-       add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
+       if (extensions != NULL) {
+          if (extensions->OES_EGL_image_external)
+             add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
+       }
 
        if (api == API_OPENGLES2)
                add_builtin_define(parser, "GL_ES", 1);
+       else {
+          add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
+          add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
 
-       if (extensions != NULL) {
-          if (extensions->EXT_texture_array) {
-             add_builtin_define(parser, "GL_EXT_texture_array", 1);
-          }
+          if (extensions != NULL) {
+             if (extensions->EXT_texture_array) {
+                add_builtin_define(parser, "GL_EXT_texture_array", 1);
+             }
+
+             if (extensions->ARB_fragment_coord_conventions)
+                add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
+                                   1);
+
+             if (extensions->ARB_explicit_attrib_location)
+                add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
 
-          if (extensions->ARB_fragment_coord_conventions)
-             add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
-                                1);
+             if (extensions->ARB_shader_texture_lod)
+                add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
 
-          if (extensions->ARB_explicit_attrib_location)
-             add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
+             if (extensions->ARB_draw_instanced)
+                add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
 
-          if (extensions->ARB_shader_texture_lod)
-             add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
+             if (extensions->ARB_conservative_depth) {
+                add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
+                add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
+             }
 
-          if (extensions->AMD_conservative_depth) {
-             add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
-             add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
+             if (extensions->ARB_shader_bit_encoding)
+                add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
+
+             if (extensions->ARB_uniform_buffer_object)
+                add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
+
+             if (extensions->ARB_texture_cube_map_array)
+                add_builtin_define(parser, "GL_ARB_texture_cube_map_array", 1);
           }
        }
 
@@ -1144,12 +1209,6 @@ glcpp_parser_create (const struct gl_extensions *extensions, int api)
        return parser;
 }
 
-int
-glcpp_parser_parse (glcpp_parser_t *parser)
-{
-       return yyparse (parser);
-}
-
 void
 glcpp_parser_destroy (glcpp_parser_t *parser)
 {
@@ -1249,32 +1308,93 @@ _arguments_parse (argument_list_t *arguments,
 }
 
 static token_list_t *
-_token_list_create_with_one_space (void *ctx)
+_token_list_create_with_one_ival (void *ctx, int type, int ival)
 {
        token_list_t *list;
-       token_t *space;
+       token_t *node;
 
        list = _token_list_create (ctx);
-       space = _token_create_ival (list, SPACE, SPACE);
-       _token_list_append (list, space);
+       node = _token_create_ival (list, type, ival);
+       _token_list_append (list, node);
 
        return list;
 }
 
+static token_list_t *
+_token_list_create_with_one_space (void *ctx)
+{
+       return _token_list_create_with_one_ival (ctx, SPACE, SPACE);
+}
+
+static token_list_t *
+_token_list_create_with_one_integer (void *ctx, int ival)
+{
+       return _token_list_create_with_one_ival (ctx, INTEGER, ival);
+}
+
+/* Perform macro expansion on 'list', placing the resulting tokens
+ * into a new list which is initialized with a first token of type
+ * 'head_token_type'. Then begin lexing from the resulting list,
+ * (return to the current lexing source when this list is exhausted).
+ */
 static void
-_glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list)
+_glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
+                                  int head_token_type,
+                                  token_list_t *list)
 {
        token_list_t *expanded;
        token_t *token;
 
        expanded = _token_list_create (parser);
-       token = _token_create_ival (parser, type, type);
+       token = _token_create_ival (parser, head_token_type, head_token_type);
        _token_list_append (expanded, token);
        _glcpp_parser_expand_token_list (parser, list);
        _token_list_append_list (expanded, list);
        glcpp_parser_lex_from (parser, expanded);
 }
 
+static void
+_glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
+{
+       token_node_t *node;
+
+       node = list->head;
+       while (node)
+       {
+               token_node_t *next_non_space;
+
+               /* Look ahead for a PASTE token, skipping space. */
+               next_non_space = node->next;
+               while (next_non_space && next_non_space->token->type == SPACE)
+                       next_non_space = next_non_space->next;
+
+               if (next_non_space == NULL)
+                       break;
+
+               if (next_non_space->token->type != PASTE) {
+                       node = next_non_space;
+                       continue;
+               }
+
+               /* Now find the next non-space token after the PASTE. */
+               next_non_space = next_non_space->next;
+               while (next_non_space && next_non_space->token->type == SPACE)
+                       next_non_space = next_non_space->next;
+
+               if (next_non_space == NULL) {
+                       yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
+                       return;
+               }
+
+               node->token = _token_paste (parser, node->token, next_non_space->token);
+               node->next = next_non_space->next;
+               if (next_non_space == list->tail)
+                       list->tail = node;
+       }
+
+       list->non_space_tail = list->tail;
+}
+
 /* This is a helper function that's essentially part of the
  * implementation of _glcpp_parser_expand_node. It shouldn't be called
  * except for by that function.
@@ -1386,43 +1506,7 @@ _glcpp_parser_expand_function (glcpp_parser_t *parser,
 
        _token_list_trim_trailing_space (substituted);
 
-       node = substituted->head;
-       while (node)
-       {
-               token_node_t *next_non_space;
-
-               /* Look ahead for a PASTE token, skipping space. */
-               next_non_space = node->next;
-               while (next_non_space && next_non_space->token->type == SPACE)
-                       next_non_space = next_non_space->next;
-
-               if (next_non_space == NULL)
-                       break;
-
-               if (next_non_space->token->type != PASTE) {
-                       node = next_non_space;
-                       continue;
-               }
-
-               /* Now find the next non-space token after the PASTE. */
-               next_non_space = next_non_space->next;
-               while (next_non_space && next_non_space->token->type == SPACE)
-                       next_non_space = next_non_space->next;
-
-               if (next_non_space == NULL) {
-                       yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
-                       return NULL;
-               }
-
-               node->token = _token_paste (parser, node->token, next_non_space->token);
-               node->next = next_non_space->next;
-               if (next_non_space == substituted->tail)
-                       substituted->tail = node;
-
-               node = node->next;
-       }
-
-       substituted->non_space_tail = substituted->tail;
+       _glcpp_parser_apply_pastes (parser, substituted);
 
        return substituted;
 }
@@ -1464,8 +1548,18 @@ _glcpp_parser_expand_node (glcpp_parser_t *parser,
                return NULL;
        }
 
-       /* Look up this identifier in the hash table. */
+       *last = node;
        identifier = token->value.str;
+
+       /* Special handling for __LINE__ and __FILE__, (not through
+        * the hash table). */
+       if (strcmp(identifier, "__LINE__") == 0)
+               return _token_list_create_with_one_integer (parser, node->token->location.first_line);
+
+       if (strcmp(identifier, "__FILE__") == 0)
+               return _token_list_create_with_one_integer (parser, node->token->location.source);
+
+       /* Look up this identifier in the hash table. */
        macro = hash_table_find (parser->defines, identifier);
 
        /* Not a macro, so no expansion needed. */
@@ -1486,19 +1580,20 @@ _glcpp_parser_expand_node (glcpp_parser_t *parser,
                final = _token_create_str (parser, OTHER, str);
                expansion = _token_list_create (parser);
                _token_list_append (expansion, final);
-               *last = node;
                return expansion;
        }
 
        if (! macro->is_function)
        {
-               *last = node;
+               token_list_t *replacement;
 
                /* Replace a macro defined as empty with a SPACE token. */
                if (macro->replacements == NULL)
                        return _token_list_create_with_one_space (parser);
 
-               return _token_list_copy (parser, macro->replacements);
+               replacement = _token_list_copy (parser, macro->replacements);
+               _glcpp_parser_apply_pastes (parser, replacement);
+               return replacement;
        }
 
        return _glcpp_parser_expand_function (parser, node, last);
@@ -1654,8 +1749,8 @@ _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
        /* According to the GLSL specification, macro names starting with "__"
         * or "GL_" are reserved for future use.  So, don't allow them.
         */
-       if (strncmp(identifier, "__", 2) == 0) {
-               glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
+       if (strstr(identifier, "__")) {
+               glcpp_error (loc, parser, "Macro names containing \"__\" are reserved.\n");
        }
        if (strncmp(identifier, "GL_", 3) == 0) {
                glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
@@ -1782,7 +1877,7 @@ glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
                        if (ret == NEWLINE)
                                parser->in_control_line = 0;
                }
-               else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
+               else if (ret == HASH_DEFINE ||
                           ret == HASH_UNDEF || ret == HASH_IF ||
                           ret == HASH_IFDEF || ret == HASH_IFNDEF ||
                           ret == HASH_ELIF || ret == HASH_ELSE ||