Home | History | Annotate | Download | only in glcpp
      1 /*
      2  * Copyright  2010 Intel Corporation
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  * and/or sell copies of the Software, and to permit persons to whom the
      9  * Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice (including the next
     12  * paragraph) shall be included in all copies or substantial portions of the
     13  * Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     21  * DEALINGS IN THE SOFTWARE.
     22  */
     23 
     24 #ifndef GLCPP_H
     25 #define GLCPP_H
     26 
     27 #include <stdint.h>
     28 #include <stdbool.h>
     29 
     30 #include "main/mtypes.h"
     31 
     32 #include "util/ralloc.h"
     33 
     34 #include "util/hash_table.h"
     35 
     36 #define yyscan_t void*
     37 
     38 /* Some data types used for parser values. */
     39 
     40 typedef struct expression_value {
     41 	intmax_t value;
     42 	char *undefined_macro;
     43 } expression_value_t;
     44 
     45 
     46 typedef struct string_node {
     47 	const char *str;
     48 	struct string_node *next;
     49 } string_node_t;
     50 
     51 typedef struct string_list {
     52 	string_node_t *head;
     53 	string_node_t *tail;
     54 } string_list_t;
     55 
     56 typedef struct token token_t;
     57 typedef struct token_list token_list_t;
     58 
     59 typedef union YYSTYPE
     60 {
     61 	intmax_t ival;
     62 	expression_value_t expression_value;
     63 	char *str;
     64 	string_list_t *string_list;
     65 	token_t *token;
     66 	token_list_t *token_list;
     67 } YYSTYPE;
     68 
     69 # define YYSTYPE_IS_TRIVIAL 1
     70 # define YYSTYPE_IS_DECLARED 1
     71 
     72 typedef struct YYLTYPE {
     73    int first_line;
     74    int first_column;
     75    int last_line;
     76    int last_column;
     77    unsigned source;
     78 } YYLTYPE;
     79 # define YYLTYPE_IS_DECLARED 1
     80 # define YYLTYPE_IS_TRIVIAL 1
     81 
     82 # define YYLLOC_DEFAULT(Current, Rhs, N)			\
     83 do {								\
     84    if (N)							\
     85    {								\
     86       (Current).first_line   = YYRHSLOC(Rhs, 1).first_line;	\
     87       (Current).first_column = YYRHSLOC(Rhs, 1).first_column;	\
     88       (Current).last_line    = YYRHSLOC(Rhs, N).last_line;	\
     89       (Current).last_column  = YYRHSLOC(Rhs, N).last_column;	\
     90    }								\
     91    else								\
     92    {								\
     93       (Current).first_line   = (Current).last_line =		\
     94 	 YYRHSLOC(Rhs, 0).last_line;				\
     95       (Current).first_column = (Current).last_column =		\
     96 	 YYRHSLOC(Rhs, 0).last_column;				\
     97    }								\
     98    (Current).source = 0;					\
     99 } while (0)
    100 
    101 struct token {
    102 	int type;
    103 	YYSTYPE value;
    104 	YYLTYPE location;
    105 };
    106 
    107 typedef struct token_node {
    108 	token_t *token;
    109 	struct token_node *next;
    110 } token_node_t;
    111 
    112 struct token_list {
    113 	token_node_t *head;
    114 	token_node_t *tail;
    115 	token_node_t *non_space_tail;
    116 };
    117 
    118 typedef struct argument_node {
    119 	token_list_t *argument;
    120 	struct argument_node *next;
    121 } argument_node_t;
    122 
    123 typedef struct argument_list {
    124 	argument_node_t *head;
    125 	argument_node_t *tail;
    126 } argument_list_t;
    127 
    128 typedef struct glcpp_parser glcpp_parser_t;
    129 
    130 typedef enum {
    131 	TOKEN_CLASS_IDENTIFIER,
    132 	TOKEN_CLASS_IDENTIFIER_FINALIZED,
    133 	TOKEN_CLASS_FUNC_MACRO,
    134 	TOKEN_CLASS_OBJ_MACRO
    135 } token_class_t;
    136 
    137 token_class_t
    138 glcpp_parser_classify_token (glcpp_parser_t *parser,
    139 			     const char *identifier,
    140 			     int *parameter_index);
    141 
    142 typedef struct {
    143 	int is_function;
    144 	string_list_t *parameters;
    145 	const char *identifier;
    146 	token_list_t *replacements;
    147 } macro_t;
    148 
    149 typedef struct expansion_node {
    150 	macro_t *macro;
    151 	token_node_t *replacements;
    152 	struct expansion_node *next;
    153 } expansion_node_t;
    154 
    155 typedef enum skip_type {
    156 	SKIP_NO_SKIP,
    157 	SKIP_TO_ELSE,
    158 	SKIP_TO_ENDIF
    159 } skip_type_t;
    160 
    161 typedef struct skip_node {
    162 	skip_type_t type;
    163 	bool has_else;
    164 	YYLTYPE loc; /* location of the initial #if/#elif/... */
    165 	struct skip_node *next;
    166 } skip_node_t;
    167 
    168 typedef struct active_list {
    169 	const char *identifier;
    170 	token_node_t *marker;
    171 	struct active_list *next;
    172 } active_list_t;
    173 
    174 struct _mesa_glsl_parse_state;
    175 
    176 typedef void (*glcpp_extension_iterator)(
    177 		struct _mesa_glsl_parse_state *state,
    178 		void (*add_builtin_define)(glcpp_parser_t *, const char *, int),
    179 		glcpp_parser_t *data,
    180 		unsigned version,
    181 		bool es);
    182 
    183 struct glcpp_parser {
    184 	void *linalloc;
    185 	yyscan_t scanner;
    186 	struct hash_table *defines;
    187 	active_list_t *active;
    188 	int lexing_directive;
    189 	int lexing_version_directive;
    190 	int space_tokens;
    191 	int last_token_was_newline;
    192 	int last_token_was_space;
    193 	int first_non_space_token_this_line;
    194 	int newline_as_space;
    195 	int in_control_line;
    196 	int paren_count;
    197 	int commented_newlines;
    198 	skip_node_t *skip_stack;
    199 	int skipping;
    200 	token_list_t *lex_from_list;
    201 	token_node_t *lex_from_node;
    202 	char *output;
    203 	char *info_log;
    204 	size_t output_length;
    205 	size_t info_log_length;
    206 	int error;
    207 	glcpp_extension_iterator extensions;
    208 	void *state;
    209 	gl_api api;
    210 	unsigned version;
    211 
    212 	/**
    213 	 * Has the #version been set?
    214 	 *
    215 	 * A separate flag is used because any possible sentinel value in
    216 	 * \c ::version could also be set by a #version line.
    217 	 */
    218 	bool version_set;
    219 
    220 	bool has_new_line_number;
    221 	int new_line_number;
    222 	bool has_new_source_number;
    223 	int new_source_number;
    224 	bool is_gles;
    225 };
    226 
    227 glcpp_parser_t *
    228 glcpp_parser_create (glcpp_extension_iterator extensions, void *state, gl_api api);
    229 
    230 int
    231 glcpp_parser_parse (glcpp_parser_t *parser);
    232 
    233 void
    234 glcpp_parser_destroy (glcpp_parser_t *parser);
    235 
    236 void
    237 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser);
    238 
    239 int
    240 glcpp_preprocess(void *ralloc_ctx, const char **shader, char **info_log,
    241 		 glcpp_extension_iterator extensions, void *state,
    242 		 struct gl_context *g_ctx);
    243 
    244 /* Functions for writing to the info log */
    245 
    246 void
    247 glcpp_error (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
    248 
    249 void
    250 glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...);
    251 
    252 /* Generated by glcpp-lex.l to glcpp-lex.c */
    253 
    254 int
    255 glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
    256 
    257 void
    258 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader);
    259 
    260 int
    261 glcpp_lex (YYSTYPE *lvalp, YYLTYPE *llocp, yyscan_t scanner);
    262 
    263 int
    264 glcpp_lex_destroy (yyscan_t scanner);
    265 
    266 /* Generated by glcpp-parse.y to glcpp-parse.c */
    267 
    268 int
    269 yyparse (glcpp_parser_t *parser);
    270 
    271 #endif
    272