2010-05-10 13:17:25 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2010 Intel Corporation
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef GLCPP_H
|
|
|
|
|
#define GLCPP_H
|
|
|
|
|
|
2010-05-24 11:27:23 -07:00
|
|
|
#include <stdint.h>
|
2012-06-09 16:31:06 -07:00
|
|
|
#include <stdbool.h>
|
2010-05-24 11:27:23 -07:00
|
|
|
|
2012-12-05 12:56:16 -08:00
|
|
|
#include "main/mtypes.h"
|
|
|
|
|
|
2011-01-21 14:32:31 -08:00
|
|
|
#include "../ralloc.h"
|
2010-05-18 22:10:04 -07:00
|
|
|
|
2010-07-29 12:40:49 +03:00
|
|
|
#include "program/hash_table.h"
|
2010-05-10 13:17:25 -07:00
|
|
|
|
|
|
|
|
#define yyscan_t void*
|
|
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
/* Some data types used for parser values. */
|
2010-05-10 16:16:06 -07:00
|
|
|
|
2010-05-14 10:05:11 -07:00
|
|
|
typedef struct string_node {
|
2010-05-12 12:17:10 -07:00
|
|
|
const char *str;
|
2010-05-14 10:05:11 -07:00
|
|
|
struct string_node *next;
|
|
|
|
|
} string_node_t;
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-14 10:05:11 -07:00
|
|
|
typedef struct string_list {
|
|
|
|
|
string_node_t *head;
|
|
|
|
|
string_node_t *tail;
|
|
|
|
|
} string_list_t;
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-25 14:52:43 -07:00
|
|
|
typedef struct token token_t;
|
|
|
|
|
typedef struct token_list token_list_t;
|
|
|
|
|
|
|
|
|
|
typedef union YYSTYPE
|
|
|
|
|
{
|
2010-05-26 09:32:57 -07:00
|
|
|
intmax_t ival;
|
2010-05-25 14:52:43 -07:00
|
|
|
char *str;
|
2010-05-25 16:28:26 -07:00
|
|
|
string_list_t *string_list;
|
2010-05-25 14:52:43 -07:00
|
|
|
token_t *token;
|
|
|
|
|
token_list_t *token_list;
|
|
|
|
|
} YYSTYPE;
|
|
|
|
|
|
|
|
|
|
# define YYSTYPE_IS_TRIVIAL 1
|
|
|
|
|
# define YYSTYPE_IS_DECLARED 1
|
|
|
|
|
|
2010-06-16 16:35:57 -07:00
|
|
|
typedef struct YYLTYPE {
|
|
|
|
|
int first_line;
|
|
|
|
|
int first_column;
|
|
|
|
|
int last_line;
|
|
|
|
|
int last_column;
|
|
|
|
|
unsigned source;
|
|
|
|
|
} YYLTYPE;
|
|
|
|
|
# define YYLTYPE_IS_DECLARED 1
|
|
|
|
|
# define YYLTYPE_IS_TRIVIAL 1
|
|
|
|
|
|
2010-07-21 13:43:47 -07:00
|
|
|
# define YYLLOC_DEFAULT(Current, Rhs, N) \
|
|
|
|
|
do { \
|
|
|
|
|
if (N) \
|
|
|
|
|
{ \
|
|
|
|
|
(Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
|
|
|
|
|
(Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
|
|
|
|
|
(Current).last_line = YYRHSLOC(Rhs, N).last_line; \
|
|
|
|
|
(Current).last_column = YYRHSLOC(Rhs, N).last_column; \
|
|
|
|
|
} \
|
|
|
|
|
else \
|
|
|
|
|
{ \
|
|
|
|
|
(Current).first_line = (Current).last_line = \
|
|
|
|
|
YYRHSLOC(Rhs, 0).last_line; \
|
|
|
|
|
(Current).first_column = (Current).last_column = \
|
|
|
|
|
YYRHSLOC(Rhs, 0).last_column; \
|
|
|
|
|
} \
|
|
|
|
|
(Current).source = 0; \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
2010-05-25 14:52:43 -07:00
|
|
|
struct token {
|
2010-05-20 08:01:44 -07:00
|
|
|
int type;
|
2010-05-25 14:52:43 -07:00
|
|
|
YYSTYPE value;
|
2010-06-16 16:58:31 -07:00
|
|
|
YYLTYPE location;
|
2010-05-25 14:52:43 -07:00
|
|
|
};
|
2010-05-20 08:01:44 -07:00
|
|
|
|
2010-05-19 13:54:37 -07:00
|
|
|
typedef struct token_node {
|
2010-05-25 14:52:43 -07:00
|
|
|
token_t *token;
|
2010-05-19 13:54:37 -07:00
|
|
|
struct token_node *next;
|
|
|
|
|
} token_node_t;
|
|
|
|
|
|
2010-05-25 14:52:43 -07:00
|
|
|
struct token_list {
|
2010-05-19 13:54:37 -07:00
|
|
|
token_node_t *head;
|
|
|
|
|
token_node_t *tail;
|
2010-05-25 20:35:01 -07:00
|
|
|
token_node_t *non_space_tail;
|
2010-05-25 14:52:43 -07:00
|
|
|
};
|
2010-05-19 13:54:37 -07:00
|
|
|
|
2010-05-14 10:44:19 -07:00
|
|
|
typedef struct argument_node {
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *argument;
|
2010-05-14 10:44:19 -07:00
|
|
|
struct argument_node *next;
|
|
|
|
|
} argument_node_t;
|
|
|
|
|
|
|
|
|
|
typedef struct argument_list {
|
|
|
|
|
argument_node_t *head;
|
|
|
|
|
argument_node_t *tail;
|
|
|
|
|
} argument_list_t;
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
typedef struct glcpp_parser glcpp_parser_t;
|
|
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
typedef enum {
|
|
|
|
|
TOKEN_CLASS_IDENTIFIER,
|
2010-05-20 08:01:44 -07:00
|
|
|
TOKEN_CLASS_IDENTIFIER_FINALIZED,
|
2010-05-18 22:10:04 -07:00
|
|
|
TOKEN_CLASS_FUNC_MACRO,
|
|
|
|
|
TOKEN_CLASS_OBJ_MACRO
|
|
|
|
|
} token_class_t;
|
|
|
|
|
|
|
|
|
|
token_class_t
|
|
|
|
|
glcpp_parser_classify_token (glcpp_parser_t *parser,
|
|
|
|
|
const char *identifier,
|
|
|
|
|
int *parameter_index);
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
int is_function;
|
|
|
|
|
string_list_t *parameters;
|
|
|
|
|
const char *identifier;
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *replacements;
|
2010-05-18 22:10:04 -07:00
|
|
|
} macro_t;
|
|
|
|
|
|
|
|
|
|
typedef struct expansion_node {
|
|
|
|
|
macro_t *macro;
|
2010-05-19 13:54:37 -07:00
|
|
|
token_node_t *replacements;
|
2010-05-18 22:10:04 -07:00
|
|
|
struct expansion_node *next;
|
|
|
|
|
} expansion_node_t;
|
|
|
|
|
|
2010-05-20 22:27:07 -07:00
|
|
|
typedef enum skip_type {
|
|
|
|
|
SKIP_NO_SKIP,
|
|
|
|
|
SKIP_TO_ELSE,
|
|
|
|
|
SKIP_TO_ENDIF
|
|
|
|
|
} skip_type_t;
|
|
|
|
|
|
|
|
|
|
typedef struct skip_node {
|
|
|
|
|
skip_type_t type;
|
2013-12-17 16:37:33 +01:00
|
|
|
bool has_else;
|
2010-06-17 12:41:46 -07:00
|
|
|
YYLTYPE loc; /* location of the initial #if/#elif/... */
|
2010-05-20 22:27:07 -07:00
|
|
|
struct skip_node *next;
|
|
|
|
|
} skip_node_t;
|
|
|
|
|
|
2010-06-02 15:32:03 -07:00
|
|
|
typedef struct active_list {
|
|
|
|
|
const char *identifier;
|
|
|
|
|
token_node_t *marker;
|
|
|
|
|
struct active_list *next;
|
|
|
|
|
} active_list_t;
|
|
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
struct glcpp_parser {
|
|
|
|
|
yyscan_t scanner;
|
|
|
|
|
struct hash_table *defines;
|
2010-06-02 15:32:03 -07:00
|
|
|
active_list_t *active;
|
2010-06-01 11:20:18 -07:00
|
|
|
int lexing_if;
|
2010-05-25 16:59:02 -07:00
|
|
|
int space_tokens;
|
Treat newlines as space when invoking a function-like macro invocation.
This adds three new pieces of state to the parser, (is_control_line,
newline_as_space, and paren_count), and a large amount of messy
code. I'd definitely like to see a cleaner solution for this.
With this fix, the "define-func-extra-newlines" now passes so we put
it back to test #26 where it was originally (lately it has been known
as test #55).
Also, we tweak test 25 slightly. Previously this test was ending a
file function-like macro name that was not actually a macro (not
followed by a left parenthesis). As is, this fix was making that test
fail because the text_line production expects to see a terminating
NEWLINE, but that NEWLINE is now getting turned into a SPACE here.
This seems unlikely to be a problem in the wild, (function macros
being used in a non-macro sense seems rare enough---but more than
likely they won't happen at the end of a file). Still, we document
this shortcoming in the README.
2010-05-26 15:57:10 -07:00
|
|
|
int newline_as_space;
|
|
|
|
|
int in_control_line;
|
|
|
|
|
int paren_count;
|
glcpp: Replace multi-line comment with a space (even as part of macro definition)
The preprocessor has always replaced multi-line comments with a single space
character, (as required by the specification), but as of commit
bd55ba568b301d0f764cd1ca015e84e1ae932c8b the lexer also emitted a NEWLINE
token for each newline within the comment, (in order to preserve line
numbers).
The emitting of NEWLINE tokens within the comment broke the rule of "replace a
multi-line comment with a single space" as could be exposed by code like the
following:
#define FOO a/*
*/b
FOO
Prior to commit bd55ba568b301d0f764cd1ca015e84e1ae932c8b, this code defined
the macro FOO as "a b" as desired. Since that commit, this code instead
defines FOO as "a" and leaves a stray "b" in the output.
In this commit, we fix this by not emitting the NEWLINE tokens while lexing
the comment, but instead merely counting them in the commented_newlines
variable. Then, when the lexer next encounters a non-commented newline it
switches to a NEWLINE_CATCHUP state to emit as many NEWLINE tokens as
necessary (so that subsequent parsing stages still generate correct line
numbers).
Of course, it would have been more clear if we could have written a loop to
emit all the newlines, but flex conventions prevent that, (we must use
"return" for each token we emit).
It similarly would have been clear to have a new rule restricted to the
<NEWLINE_CATCHUP> state with an action much like the body of this if
condition. The problem with that is that this rule must not consume any
characters. It might be possible to write a rule that matches a single
lookahead of any character, but then we would also need an additional rule to
ensure for the <EOF> case where there are no additional characters available
for the lookahead to match.
Given those considerations, and given that the SKIP-state manipulation already
involves a code block at the top of the lexer function, before any rules, it
seems best to me to go with the implementation here which adds a similar
pre-rule code block for the NEWLINE_CATCHUP.
Finally, this commit also changes the expected output of a few, existing glcpp
tests. The change here is that the space character resulting from the
multi-line comment is now emitted before the newlines corresponding to that
comment. (Previously, the newlines were emitted first, and the space character
afterward.)
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=72686
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2013-12-19 16:06:31 -08:00
|
|
|
int commented_newlines;
|
2010-05-20 22:27:07 -07:00
|
|
|
skip_node_t *skip_stack;
|
2010-05-26 11:15:21 -07:00
|
|
|
token_list_t *lex_from_list;
|
|
|
|
|
token_node_t *lex_from_node;
|
2010-06-16 11:57:48 -07:00
|
|
|
char *output;
|
2010-06-18 19:52:36 -07:00
|
|
|
char *info_log;
|
2012-02-09 20:33:44 -08:00
|
|
|
size_t output_length;
|
|
|
|
|
size_t info_log_length;
|
2010-06-21 12:39:49 -07:00
|
|
|
int error;
|
2012-06-09 16:31:06 -07:00
|
|
|
bool has_new_line_number;
|
|
|
|
|
int new_line_number;
|
|
|
|
|
bool has_new_source_number;
|
|
|
|
|
int new_source_number;
|
2012-11-26 14:53:54 -08:00
|
|
|
bool is_gles;
|
2010-05-18 22:10:04 -07:00
|
|
|
};
|
|
|
|
|
|
2010-06-30 16:27:22 -07:00
|
|
|
struct gl_extensions;
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
glcpp_parser_t *
|
2010-09-05 01:27:02 -07:00
|
|
|
glcpp_parser_create (const struct gl_extensions *extensions, int api);
|
2010-05-10 16:16:06 -07:00
|
|
|
|
|
|
|
|
int
|
|
|
|
|
glcpp_parser_parse (glcpp_parser_t *parser);
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-12 12:17:10 -07:00
|
|
|
glcpp_parser_destroy (glcpp_parser_t *parser);
|
2010-05-10 16:16:06 -07:00
|
|
|
|
2010-06-23 14:00:27 -07:00
|
|
|
int
|
2012-09-14 10:13:01 +10:00
|
|
|
glcpp_preprocess(void *ralloc_ctx, const char **shader, char **info_log,
|
2012-12-05 12:56:16 -08:00
|
|
|
const struct gl_extensions *extensions, struct gl_context *g_ctx);
|
2010-06-23 14:00:27 -07:00
|
|
|
|
2010-06-18 19:54:25 -07:00
|
|
|
/* Functions for writing to the info log */
|
|
|
|
|
|
2010-06-17 12:03:25 -07:00
|
|
|
void
|
|
|
|
|
glcpp_error (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
|
|
|
|
|
|
2010-06-18 19:54:25 -07:00
|
|
|
void
|
|
|
|
|
glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
|
|
|
|
|
|
2010-05-10 16:16:06 -07:00
|
|
|
/* Generated by glcpp-lex.l to glcpp-lex.c */
|
|
|
|
|
|
2010-05-10 13:17:25 -07:00
|
|
|
int
|
2010-05-19 10:01:29 -07:00
|
|
|
glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
|
2010-05-10 13:17:25 -07:00
|
|
|
|
2010-06-16 12:01:17 -07:00
|
|
|
void
|
|
|
|
|
glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader);
|
|
|
|
|
|
2010-05-10 13:17:25 -07:00
|
|
|
int
|
2010-06-16 16:35:57 -07:00
|
|
|
glcpp_lex (YYSTYPE *lvalp, YYLTYPE *llocp, yyscan_t scanner);
|
2010-05-10 13:17:25 -07:00
|
|
|
|
|
|
|
|
int
|
2010-05-19 10:01:29 -07:00
|
|
|
glcpp_lex_destroy (yyscan_t scanner);
|
2010-05-10 13:17:25 -07:00
|
|
|
|
|
|
|
|
/* Generated by glcpp-parse.y to glcpp-parse.c */
|
|
|
|
|
|
|
|
|
|
int
|
2010-05-10 16:16:06 -07:00
|
|
|
yyparse (glcpp_parser_t *parser);
|
2010-05-10 13:17:25 -07:00
|
|
|
|
|
|
|
|
#endif
|