Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 1 | %{ |
| 2 | /* |
| 3 | * Copyright © 2010 Intel Corporation |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the next |
| 13 | * paragraph) shall be included in all copies or substantial portions of the |
| 14 | * Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Ian Romanick | 4ca4edd | 2010-08-12 10:07:29 -0700 | [diff] [blame] | 27 | #include <string.h> |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 28 | #include <assert.h> |
José Fonseca | 9349379 | 2010-08-14 16:01:24 +0100 | [diff] [blame] | 29 | #include <inttypes.h> |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 30 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 31 | #include "glcpp.h" |
Ian Romanick | 06143ea | 2010-06-30 16:27:22 -0700 | [diff] [blame] | 32 | #include "main/mtypes.h" |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 33 | |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 34 | #define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str) |
Vinson Lee | 07ca55b | 2010-08-13 17:11:21 -0700 | [diff] [blame] | 35 | #define glcpp_printf(stream, fmt, args, ...) \ |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 36 | stream = talloc_asprintf_append(stream, fmt, args) |
| 37 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 38 | static void |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 39 | yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 40 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 41 | static void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 42 | _define_object_macro (glcpp_parser_t *parser, |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 43 | YYLTYPE *loc, |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 44 | const char *macro, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 45 | token_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 46 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 47 | static void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 48 | _define_function_macro (glcpp_parser_t *parser, |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 49 | YYLTYPE *loc, |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 50 | const char *macro, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 51 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 52 | token_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 53 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 54 | static string_list_t * |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 55 | _string_list_create (void *ctx); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 56 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 57 | static void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 58 | _string_list_append_item (string_list_t *list, const char *str); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 59 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 60 | static int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 61 | _string_list_contains (string_list_t *list, const char *member, int *index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 62 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 63 | static int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 64 | _string_list_length (string_list_t *list); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 65 | |
Carl Worth | 3882cf2 | 2010-08-17 23:20:58 -0700 | [diff] [blame] | 66 | static int |
| 67 | _string_list_equal (string_list_t *a, string_list_t *b); |
| 68 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 69 | static argument_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 70 | _argument_list_create (void *ctx); |
| 71 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 72 | static void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 73 | _argument_list_append (argument_list_t *list, token_list_t *argument); |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 74 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 75 | static int |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 76 | _argument_list_length (argument_list_t *list); |
| 77 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 78 | static token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 79 | _argument_list_member_at (argument_list_t *list, int index); |
| 80 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 81 | /* Note: This function talloc_steal()s the str pointer. */ |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 82 | static token_t * |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 83 | _token_create_str (void *ctx, int type, char *str); |
| 84 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 85 | static token_t * |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 86 | _token_create_ival (void *ctx, int type, int ival); |
| 87 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 88 | static token_list_t * |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 89 | _token_list_create (void *ctx); |
| 90 | |
Carl Worth | b1ae61a | 2010-05-26 08:10:38 -0700 | [diff] [blame] | 91 | /* Note: This function adds a talloc_reference() to token. |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 92 | * |
| 93 | * You may want to talloc_unlink any current reference if you no |
| 94 | * longer need it. */ |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 95 | static void |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 96 | _token_list_append (token_list_t *list, token_t *token); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 97 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 98 | static void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 99 | _token_list_append_list (token_list_t *list, token_list_t *tail); |
| 100 | |
Carl Worth | 3882cf2 | 2010-08-17 23:20:58 -0700 | [diff] [blame] | 101 | static int |
| 102 | _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b); |
| 103 | |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 104 | static active_list_t * |
| 105 | _active_list_push (active_list_t *list, |
| 106 | const char *identifier, |
| 107 | token_node_t *marker); |
| 108 | |
| 109 | static active_list_t * |
| 110 | _active_list_pop (active_list_t *list); |
| 111 | |
| 112 | int |
| 113 | _active_list_contains (active_list_t *list, const char *identifier); |
| 114 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 115 | static void |
Kenneth Graunke | 16b4eed | 2010-08-04 16:10:03 -0700 | [diff] [blame] | 116 | _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list); |
| 117 | |
| 118 | static void |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 119 | _glcpp_parser_expand_token_list (glcpp_parser_t *parser, |
| 120 | token_list_t *list); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 121 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 122 | static void |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 123 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 124 | token_list_t *list); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 125 | |
| 126 | static void |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 127 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc, |
| 128 | int condition); |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 129 | |
| 130 | static void |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 131 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc, |
| 132 | const char *type, int condition); |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 133 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 134 | static void |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 135 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc); |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 136 | |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 137 | #define yylex glcpp_parser_lex |
| 138 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 139 | static int |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 140 | glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 141 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 142 | static void |
| 143 | glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list); |
| 144 | |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 145 | static void |
| 146 | add_builtin_define(glcpp_parser_t *parser, const char *name, int value); |
| 147 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 148 | %} |
| 149 | |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 150 | %pure-parser |
Kenneth Graunke | f70f607 | 2010-06-17 13:07:13 -0700 | [diff] [blame] | 151 | %error-verbose |
Carl Worth | 485f84d | 2010-08-10 16:58:28 -0700 | [diff] [blame] | 152 | |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 153 | %locations |
Carl Worth | 485f84d | 2010-08-10 16:58:28 -0700 | [diff] [blame] | 154 | %initial-action { |
| 155 | @$.first_line = 1; |
| 156 | @$.first_column = 1; |
| 157 | @$.last_line = 1; |
| 158 | @$.last_column = 1; |
| 159 | @$.source = 0; |
| 160 | } |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 161 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 162 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 163 | %lex-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 164 | |
Carl Worth | efef950 | 2010-07-28 11:10:52 -0700 | [diff] [blame] | 165 | %expect 0 |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 166 | %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 |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 167 | %token PASTE |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 168 | %type <ival> expression INTEGER operator SPACE integer_constant |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 169 | %type <str> IDENTIFIER INTEGER_STRING OTHER |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 170 | %type <string_list> identifier_list |
Kenneth Graunke | 26e761e | 2010-06-18 23:06:54 -0700 | [diff] [blame] | 171 | %type <token> preprocessing_token conditional_token |
| 172 | %type <token_list> pp_tokens replacement_list text_line conditional_tokens |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 173 | %left OR |
| 174 | %left AND |
| 175 | %left '|' |
| 176 | %left '^' |
| 177 | %left '&' |
| 178 | %left EQUAL NOT_EQUAL |
| 179 | %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL |
| 180 | %left LEFT_SHIFT RIGHT_SHIFT |
| 181 | %left '+' '-' |
| 182 | %left '*' '/' '%' |
| 183 | %right UNARY |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 184 | |
| 185 | %% |
| 186 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 187 | input: |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 188 | /* empty */ |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 189 | | input line |
| 190 | ; |
| 191 | |
| 192 | line: |
| 193 | control_line { |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 194 | glcpp_print(parser->output, "\n"); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 195 | } |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 196 | | text_line { |
Carl Worth | a771a40 | 2010-06-01 11:20:18 -0700 | [diff] [blame] | 197 | _glcpp_parser_print_expanded_token_list (parser, $1); |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 198 | glcpp_print(parser->output, "\n"); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 199 | talloc_free ($1); |
| 200 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 201 | | expanded_line |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 202 | | HASH non_directive |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 203 | ; |
| 204 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 205 | expanded_line: |
| 206 | IF_EXPANDED expression NEWLINE { |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 207 | _glcpp_parser_skip_stack_push_if (parser, & @1, $2); |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 208 | } |
| 209 | | ELIF_EXPANDED expression NEWLINE { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 210 | _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2); |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 211 | } |
| 212 | ; |
| 213 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 214 | control_line: |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 215 | HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 216 | _define_object_macro (parser, & @2, $2, $3); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 217 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 218 | | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 219 | _define_function_macro (parser, & @2, $2, NULL, $5); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 220 | } |
| 221 | | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 222 | _define_function_macro (parser, & @2, $2, $4, $6); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 223 | } |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame] | 224 | | HASH_UNDEF IDENTIFIER NEWLINE { |
Carl Worth | 0324cad | 2010-05-26 15:53:05 -0700 | [diff] [blame] | 225 | macro_t *macro = hash_table_find (parser->defines, $2); |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame] | 226 | if (macro) { |
Carl Worth | 61ebc01 | 2010-07-19 18:02:12 -0700 | [diff] [blame] | 227 | hash_table_remove (parser->defines, $2); |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame] | 228 | talloc_free (macro); |
| 229 | } |
| 230 | talloc_free ($2); |
| 231 | } |
Kenneth Graunke | 26e761e | 2010-06-18 23:06:54 -0700 | [diff] [blame] | 232 | | HASH_IF conditional_tokens NEWLINE { |
Carl Worth | 48ba058 | 2010-08-11 12:43:44 -0700 | [diff] [blame] | 233 | /* Be careful to only evaluate the 'if' expression if |
| 234 | * we are not skipping. When we are skipping, we |
| 235 | * simply push a new 0-valued 'if' onto the skip |
| 236 | * stack. |
| 237 | * |
| 238 | * This avoids generating diagnostics for invalid |
| 239 | * expressions that are being skipped. */ |
| 240 | if (parser->skip_stack == NULL || |
| 241 | parser->skip_stack->type == SKIP_NO_SKIP) |
| 242 | { |
| 243 | _glcpp_parser_expand_if (parser, IF_EXPANDED, $2); |
| 244 | } |
| 245 | else |
| 246 | { |
Kenneth Graunke | f423987 | 2010-08-04 16:24:39 -0700 | [diff] [blame] | 247 | _glcpp_parser_skip_stack_push_if (parser, & @1, 0); |
| 248 | parser->skip_stack->type = SKIP_TO_ENDIF; |
Kenneth Graunke | f423987 | 2010-08-04 16:24:39 -0700 | [diff] [blame] | 249 | } |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 250 | } |
Carl Worth | 253cad3 | 2010-08-11 13:59:22 -0700 | [diff] [blame] | 251 | | HASH_IF NEWLINE { |
| 252 | /* #if without an expression is only an error if we |
| 253 | * are not skipping */ |
| 254 | if (parser->skip_stack == NULL || |
| 255 | parser->skip_stack->type == SKIP_NO_SKIP) |
| 256 | { |
| 257 | glcpp_error(& @1, parser, "#if with no expression"); |
| 258 | } |
| 259 | _glcpp_parser_skip_stack_push_if (parser, & @1, 0); |
| 260 | } |
Kenneth Graunke | 6587574 | 2010-06-18 20:08:15 -0700 | [diff] [blame] | 261 | | HASH_IFDEF IDENTIFIER junk NEWLINE { |
Carl Worth | 0324cad | 2010-05-26 15:53:05 -0700 | [diff] [blame] | 262 | macro_t *macro = hash_table_find (parser->defines, $2); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 263 | talloc_free ($2); |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 264 | _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 265 | } |
Kenneth Graunke | 6587574 | 2010-06-18 20:08:15 -0700 | [diff] [blame] | 266 | | HASH_IFNDEF IDENTIFIER junk NEWLINE { |
Carl Worth | 0324cad | 2010-05-26 15:53:05 -0700 | [diff] [blame] | 267 | macro_t *macro = hash_table_find (parser->defines, $2); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 268 | talloc_free ($2); |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 269 | _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 270 | } |
Kenneth Graunke | 26e761e | 2010-06-18 23:06:54 -0700 | [diff] [blame] | 271 | | HASH_ELIF conditional_tokens NEWLINE { |
Carl Worth | 48ba058 | 2010-08-11 12:43:44 -0700 | [diff] [blame] | 272 | /* Be careful to only evaluate the 'elif' expression |
| 273 | * if we are not skipping. When we are skipping, we |
| 274 | * simply change to a 0-valued 'elif' on the skip |
| 275 | * stack. |
| 276 | * |
| 277 | * This avoids generating diagnostics for invalid |
| 278 | * expressions that are being skipped. */ |
| 279 | if (parser->skip_stack && |
| 280 | parser->skip_stack->type == SKIP_TO_ELSE) |
| 281 | { |
Kenneth Graunke | f423987 | 2010-08-04 16:24:39 -0700 | [diff] [blame] | 282 | _glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2); |
Carl Worth | 48ba058 | 2010-08-11 12:43:44 -0700 | [diff] [blame] | 283 | } |
| 284 | else |
| 285 | { |
| 286 | _glcpp_parser_skip_stack_change_if (parser, & @1, |
| 287 | "elif", 0); |
| 288 | } |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 289 | } |
Kenneth Graunke | 332fc47 | 2010-06-21 12:20:22 -0700 | [diff] [blame] | 290 | | HASH_ELIF NEWLINE { |
Carl Worth | 48ba058 | 2010-08-11 12:43:44 -0700 | [diff] [blame] | 291 | /* #elif without an expression is an error unless we |
| 292 | * are skipping. */ |
| 293 | if (parser->skip_stack && |
| 294 | parser->skip_stack->type == SKIP_TO_ELSE) |
| 295 | { |
Carl Worth | 624dd58 | 2010-08-11 13:50:51 -0700 | [diff] [blame] | 296 | glcpp_error(& @1, parser, "#elif with no expression"); |
Kenneth Graunke | 332fc47 | 2010-06-21 12:20:22 -0700 | [diff] [blame] | 297 | } |
Carl Worth | 48ba058 | 2010-08-11 12:43:44 -0700 | [diff] [blame] | 298 | else |
| 299 | { |
| 300 | _glcpp_parser_skip_stack_change_if (parser, & @1, |
| 301 | "elif", 0); |
| 302 | glcpp_warning(& @1, parser, "ignoring illegal #elif without expression"); |
| 303 | } |
Kenneth Graunke | 332fc47 | 2010-06-21 12:20:22 -0700 | [diff] [blame] | 304 | } |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 305 | | HASH_ELSE NEWLINE { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 306 | _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 307 | } |
| 308 | | HASH_ENDIF NEWLINE { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 309 | _glcpp_parser_skip_stack_pop (parser, & @1); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 310 | } |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 311 | | HASH_VERSION integer_constant NEWLINE { |
| 312 | macro_t *macro = hash_table_find (parser->defines, "__VERSION__"); |
| 313 | if (macro) { |
| 314 | hash_table_remove (parser->defines, "__VERSION__"); |
| 315 | talloc_free (macro); |
| 316 | } |
| 317 | add_builtin_define (parser, "__VERSION__", $2); |
Kenneth Graunke | 6be3a8b | 2010-08-16 13:42:04 -0700 | [diff] [blame] | 318 | glcpp_printf(parser->output, "#version %" PRIiMAX, $2); |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 319 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 320 | | HASH NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 321 | ; |
| 322 | |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 323 | integer_constant: |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 324 | INTEGER_STRING { |
| 325 | if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) { |
| 326 | $$ = strtoll ($1 + 2, NULL, 16); |
| 327 | } else if ($1[0] == '0') { |
| 328 | $$ = strtoll ($1, NULL, 8); |
| 329 | } else { |
| 330 | $$ = strtoll ($1, NULL, 10); |
| 331 | } |
| 332 | } |
| 333 | | INTEGER { |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 334 | $$ = $1; |
| 335 | } |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 336 | |
| 337 | expression: |
| 338 | integer_constant |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 339 | | expression OR expression { |
| 340 | $$ = $1 || $3; |
| 341 | } |
| 342 | | expression AND expression { |
| 343 | $$ = $1 && $3; |
| 344 | } |
| 345 | | expression '|' expression { |
| 346 | $$ = $1 | $3; |
| 347 | } |
| 348 | | expression '^' expression { |
| 349 | $$ = $1 ^ $3; |
| 350 | } |
| 351 | | expression '&' expression { |
| 352 | $$ = $1 & $3; |
| 353 | } |
| 354 | | expression NOT_EQUAL expression { |
| 355 | $$ = $1 != $3; |
| 356 | } |
| 357 | | expression EQUAL expression { |
| 358 | $$ = $1 == $3; |
| 359 | } |
| 360 | | expression GREATER_OR_EQUAL expression { |
| 361 | $$ = $1 >= $3; |
| 362 | } |
| 363 | | expression LESS_OR_EQUAL expression { |
| 364 | $$ = $1 <= $3; |
| 365 | } |
| 366 | | expression '>' expression { |
| 367 | $$ = $1 > $3; |
| 368 | } |
| 369 | | expression '<' expression { |
| 370 | $$ = $1 < $3; |
| 371 | } |
| 372 | | expression RIGHT_SHIFT expression { |
| 373 | $$ = $1 >> $3; |
| 374 | } |
| 375 | | expression LEFT_SHIFT expression { |
| 376 | $$ = $1 << $3; |
| 377 | } |
| 378 | | expression '-' expression { |
| 379 | $$ = $1 - $3; |
| 380 | } |
| 381 | | expression '+' expression { |
| 382 | $$ = $1 + $3; |
| 383 | } |
| 384 | | expression '%' expression { |
| 385 | $$ = $1 % $3; |
| 386 | } |
| 387 | | expression '/' expression { |
| 388 | $$ = $1 / $3; |
| 389 | } |
| 390 | | expression '*' expression { |
| 391 | $$ = $1 * $3; |
| 392 | } |
| 393 | | '!' expression %prec UNARY { |
| 394 | $$ = ! $2; |
| 395 | } |
| 396 | | '~' expression %prec UNARY { |
| 397 | $$ = ~ $2; |
| 398 | } |
| 399 | | '-' expression %prec UNARY { |
| 400 | $$ = - $2; |
| 401 | } |
| 402 | | '+' expression %prec UNARY { |
| 403 | $$ = + $2; |
| 404 | } |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 405 | | '(' expression ')' { |
| 406 | $$ = $2; |
| 407 | } |
| 408 | ; |
| 409 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 410 | identifier_list: |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 411 | IDENTIFIER { |
| 412 | $$ = _string_list_create (parser); |
| 413 | _string_list_append_item ($$, $1); |
| 414 | talloc_steal ($$, $1); |
| 415 | } |
| 416 | | identifier_list ',' IDENTIFIER { |
| 417 | $$ = $1; |
| 418 | _string_list_append_item ($$, $3); |
| 419 | talloc_steal ($$, $3); |
| 420 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 421 | ; |
| 422 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 423 | text_line: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 424 | NEWLINE { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 425 | | pp_tokens NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 426 | ; |
| 427 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 428 | non_directive: |
Kenneth Graunke | 739ba06 | 2010-06-16 12:41:37 -0700 | [diff] [blame] | 429 | pp_tokens NEWLINE { |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 430 | yyerror (& @1, parser, "Invalid tokens after #"); |
Kenneth Graunke | 739ba06 | 2010-06-16 12:41:37 -0700 | [diff] [blame] | 431 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 432 | ; |
| 433 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 434 | replacement_list: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 435 | /* empty */ { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 436 | | pp_tokens |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 437 | ; |
| 438 | |
Kenneth Graunke | 6587574 | 2010-06-18 20:08:15 -0700 | [diff] [blame] | 439 | junk: |
| 440 | /* empty */ |
| 441 | | pp_tokens { |
| 442 | glcpp_warning(&@1, parser, "extra tokens at end of directive"); |
| 443 | } |
Kenneth Graunke | 26e761e | 2010-06-18 23:06:54 -0700 | [diff] [blame] | 444 | ; |
| 445 | |
| 446 | conditional_token: |
| 447 | /* Handle "defined" operator */ |
| 448 | DEFINED IDENTIFIER { |
| 449 | int v = hash_table_find (parser->defines, $2) ? 1 : 0; |
| 450 | $$ = _token_create_ival (parser, INTEGER, v); |
| 451 | } |
| 452 | | DEFINED '(' IDENTIFIER ')' { |
| 453 | int v = hash_table_find (parser->defines, $3) ? 1 : 0; |
| 454 | $$ = _token_create_ival (parser, INTEGER, v); |
| 455 | } |
| 456 | | preprocessing_token |
| 457 | ; |
| 458 | |
| 459 | conditional_tokens: |
| 460 | /* Exactly the same as pp_tokens, but using conditional_token */ |
| 461 | conditional_token { |
| 462 | parser->space_tokens = 1; |
| 463 | $$ = _token_list_create (parser); |
| 464 | _token_list_append ($$, $1); |
| 465 | talloc_unlink (parser, $1); |
| 466 | } |
| 467 | | conditional_tokens conditional_token { |
| 468 | $$ = $1; |
| 469 | _token_list_append ($$, $2); |
| 470 | talloc_unlink (parser, $2); |
| 471 | } |
| 472 | ; |
Kenneth Graunke | 6587574 | 2010-06-18 20:08:15 -0700 | [diff] [blame] | 473 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 474 | pp_tokens: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 475 | preprocessing_token { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 476 | parser->space_tokens = 1; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 477 | $$ = _token_list_create (parser); |
| 478 | _token_list_append ($$, $1); |
| 479 | talloc_unlink (parser, $1); |
| 480 | } |
| 481 | | pp_tokens preprocessing_token { |
| 482 | $$ = $1; |
| 483 | _token_list_append ($$, $2); |
| 484 | talloc_unlink (parser, $2); |
| 485 | } |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 486 | ; |
| 487 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 488 | preprocessing_token: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 489 | IDENTIFIER { |
| 490 | $$ = _token_create_str (parser, IDENTIFIER, $1); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 491 | $$->location = yylloc; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 492 | } |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 493 | | INTEGER_STRING { |
| 494 | $$ = _token_create_str (parser, INTEGER_STRING, $1); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 495 | $$->location = yylloc; |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 496 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 497 | | operator { |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 498 | $$ = _token_create_ival (parser, $1, $1); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 499 | $$->location = yylloc; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 500 | } |
| 501 | | OTHER { |
| 502 | $$ = _token_create_str (parser, OTHER, $1); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 503 | $$->location = yylloc; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 504 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 505 | | SPACE { |
Carl Worth | e939786 | 2010-05-25 17:08:07 -0700 | [diff] [blame] | 506 | $$ = _token_create_ival (parser, SPACE, SPACE); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 507 | $$->location = yylloc; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 508 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 509 | ; |
| 510 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 511 | operator: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 512 | '[' { $$ = '['; } |
| 513 | | ']' { $$ = ']'; } |
| 514 | | '(' { $$ = '('; } |
| 515 | | ')' { $$ = ')'; } |
| 516 | | '{' { $$ = '{'; } |
| 517 | | '}' { $$ = '}'; } |
| 518 | | '.' { $$ = '.'; } |
| 519 | | '&' { $$ = '&'; } |
| 520 | | '*' { $$ = '*'; } |
| 521 | | '+' { $$ = '+'; } |
| 522 | | '-' { $$ = '-'; } |
| 523 | | '~' { $$ = '~'; } |
| 524 | | '!' { $$ = '!'; } |
| 525 | | '/' { $$ = '/'; } |
| 526 | | '%' { $$ = '%'; } |
| 527 | | LEFT_SHIFT { $$ = LEFT_SHIFT; } |
| 528 | | RIGHT_SHIFT { $$ = RIGHT_SHIFT; } |
| 529 | | '<' { $$ = '<'; } |
| 530 | | '>' { $$ = '>'; } |
| 531 | | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; } |
| 532 | | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; } |
| 533 | | EQUAL { $$ = EQUAL; } |
| 534 | | NOT_EQUAL { $$ = NOT_EQUAL; } |
| 535 | | '^' { $$ = '^'; } |
| 536 | | '|' { $$ = '|'; } |
| 537 | | AND { $$ = AND; } |
| 538 | | OR { $$ = OR; } |
| 539 | | ';' { $$ = ';'; } |
| 540 | | ',' { $$ = ','; } |
Carl Worth | 6310169 | 2010-05-29 05:07:24 -0700 | [diff] [blame] | 541 | | '=' { $$ = '='; } |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 542 | | PASTE { $$ = PASTE; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 543 | ; |
| 544 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 545 | %% |
| 546 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 547 | string_list_t * |
| 548 | _string_list_create (void *ctx) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 549 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 550 | string_list_t *list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 551 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 552 | list = talloc (ctx, string_list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 553 | list->head = NULL; |
| 554 | list->tail = NULL; |
| 555 | |
| 556 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 557 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 558 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 559 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 560 | _string_list_append_item (string_list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 561 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 562 | string_node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 563 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 564 | node = talloc (list, string_node_t); |
| 565 | node->str = talloc_strdup (node, str); |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 566 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 567 | node->next = NULL; |
| 568 | |
| 569 | if (list->head == NULL) { |
| 570 | list->head = node; |
| 571 | } else { |
| 572 | list->tail->next = node; |
| 573 | } |
| 574 | |
| 575 | list->tail = node; |
| 576 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 577 | |
| 578 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 579 | _string_list_contains (string_list_t *list, const char *member, int *index) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 580 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 581 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 582 | int i; |
| 583 | |
| 584 | if (list == NULL) |
| 585 | return 0; |
| 586 | |
| 587 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 588 | if (strcmp (node->str, member) == 0) { |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 589 | if (index) |
| 590 | *index = i; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 591 | return 1; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 599 | _string_list_length (string_list_t *list) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 600 | { |
| 601 | int length = 0; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 602 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 603 | |
| 604 | if (list == NULL) |
| 605 | return 0; |
| 606 | |
| 607 | for (node = list->head; node; node = node->next) |
| 608 | length++; |
| 609 | |
| 610 | return length; |
| 611 | } |
| 612 | |
Carl Worth | 3882cf2 | 2010-08-17 23:20:58 -0700 | [diff] [blame] | 613 | int |
| 614 | _string_list_equal (string_list_t *a, string_list_t *b) |
| 615 | { |
| 616 | string_node_t *node_a, *node_b; |
| 617 | |
| 618 | if (a == NULL && b == NULL) |
| 619 | return 1; |
| 620 | |
| 621 | if (a == NULL || b == NULL) |
| 622 | return 0; |
| 623 | |
| 624 | for (node_a = a->head, node_b = b->head; |
| 625 | node_a && node_b; |
| 626 | node_a = node_a->next, node_b = node_b->next) |
| 627 | { |
| 628 | if (strcmp (node_a->str, node_b->str)) |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | /* Catch the case of lists being different lengths, (which |
| 633 | * would cause the loop above to terminate after the shorter |
| 634 | * list). */ |
| 635 | return node_a == node_b; |
| 636 | } |
| 637 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 638 | argument_list_t * |
| 639 | _argument_list_create (void *ctx) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 640 | { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 641 | argument_list_t *list; |
| 642 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 643 | list = talloc (ctx, argument_list_t); |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 644 | list->head = NULL; |
| 645 | list->tail = NULL; |
| 646 | |
| 647 | return list; |
| 648 | } |
| 649 | |
| 650 | void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 651 | _argument_list_append (argument_list_t *list, token_list_t *argument) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 652 | { |
| 653 | argument_node_t *node; |
| 654 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 655 | node = talloc (list, argument_node_t); |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 656 | node->argument = argument; |
| 657 | |
| 658 | node->next = NULL; |
| 659 | |
| 660 | if (list->head == NULL) { |
| 661 | list->head = node; |
| 662 | } else { |
| 663 | list->tail->next = node; |
| 664 | } |
| 665 | |
| 666 | list->tail = node; |
| 667 | } |
| 668 | |
| 669 | int |
| 670 | _argument_list_length (argument_list_t *list) |
| 671 | { |
| 672 | int length = 0; |
| 673 | argument_node_t *node; |
| 674 | |
| 675 | if (list == NULL) |
| 676 | return 0; |
| 677 | |
| 678 | for (node = list->head; node; node = node->next) |
| 679 | length++; |
| 680 | |
| 681 | return length; |
| 682 | } |
| 683 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 684 | token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 685 | _argument_list_member_at (argument_list_t *list, int index) |
| 686 | { |
| 687 | argument_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 688 | int i; |
| 689 | |
| 690 | if (list == NULL) |
| 691 | return NULL; |
| 692 | |
| 693 | node = list->head; |
| 694 | for (i = 0; i < index; i++) { |
| 695 | node = node->next; |
| 696 | if (node == NULL) |
| 697 | break; |
| 698 | } |
| 699 | |
| 700 | if (node) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 701 | return node->argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 702 | |
| 703 | return NULL; |
| 704 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 705 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 706 | /* Note: This function talloc_steal()s the str pointer. */ |
| 707 | token_t * |
| 708 | _token_create_str (void *ctx, int type, char *str) |
| 709 | { |
| 710 | token_t *token; |
| 711 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 712 | token = talloc (ctx, token_t); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 713 | token->type = type; |
| 714 | token->value.str = talloc_steal (token, str); |
| 715 | |
| 716 | return token; |
| 717 | } |
| 718 | |
| 719 | token_t * |
| 720 | _token_create_ival (void *ctx, int type, int ival) |
| 721 | { |
| 722 | token_t *token; |
| 723 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 724 | token = talloc (ctx, token_t); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 725 | token->type = type; |
| 726 | token->value.ival = ival; |
| 727 | |
| 728 | return token; |
| 729 | } |
| 730 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 731 | token_list_t * |
| 732 | _token_list_create (void *ctx) |
| 733 | { |
| 734 | token_list_t *list; |
| 735 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 736 | list = talloc (ctx, token_list_t); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 737 | list->head = NULL; |
| 738 | list->tail = NULL; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 739 | list->non_space_tail = NULL; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 740 | |
| 741 | return list; |
| 742 | } |
| 743 | |
| 744 | void |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 745 | _token_list_append (token_list_t *list, token_t *token) |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 746 | { |
| 747 | token_node_t *node; |
| 748 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 749 | node = talloc (list, token_node_t); |
| 750 | node->token = talloc_reference (list, token); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 751 | |
| 752 | node->next = NULL; |
| 753 | |
| 754 | if (list->head == NULL) { |
| 755 | list->head = node; |
| 756 | } else { |
| 757 | list->tail->next = node; |
| 758 | } |
| 759 | |
| 760 | list->tail = node; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 761 | if (token->type != SPACE) |
| 762 | list->non_space_tail = node; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void |
| 766 | _token_list_append_list (token_list_t *list, token_list_t *tail) |
| 767 | { |
Carl Worth | a65cf7b | 2010-05-27 11:55:36 -0700 | [diff] [blame] | 768 | if (tail == NULL || tail->head == NULL) |
| 769 | return; |
| 770 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 771 | if (list->head == NULL) { |
| 772 | list->head = tail->head; |
| 773 | } else { |
| 774 | list->tail->next = tail->head; |
| 775 | } |
| 776 | |
| 777 | list->tail = tail->tail; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 778 | list->non_space_tail = tail->non_space_tail; |
| 779 | } |
| 780 | |
Carl Worth | d80dcaf | 2010-07-20 15:55:21 -0700 | [diff] [blame] | 781 | static token_list_t * |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 782 | _token_list_copy (void *ctx, token_list_t *other) |
| 783 | { |
| 784 | token_list_t *copy; |
| 785 | token_node_t *node; |
| 786 | |
| 787 | if (other == NULL) |
| 788 | return NULL; |
| 789 | |
| 790 | copy = _token_list_create (ctx); |
| 791 | for (node = other->head; node; node = node->next) |
| 792 | _token_list_append (copy, node->token); |
| 793 | |
| 794 | return copy; |
| 795 | } |
| 796 | |
Carl Worth | d80dcaf | 2010-07-20 15:55:21 -0700 | [diff] [blame] | 797 | static void |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 798 | _token_list_trim_trailing_space (token_list_t *list) |
| 799 | { |
| 800 | token_node_t *tail, *next; |
| 801 | |
| 802 | if (list->non_space_tail) { |
| 803 | tail = list->non_space_tail->next; |
| 804 | list->non_space_tail->next = NULL; |
| 805 | list->tail = list->non_space_tail; |
| 806 | |
| 807 | while (tail) { |
| 808 | next = tail->next; |
| 809 | talloc_free (tail); |
| 810 | tail = next; |
| 811 | } |
| 812 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 813 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 814 | |
Carl Worth | 3882cf2 | 2010-08-17 23:20:58 -0700 | [diff] [blame] | 815 | int |
| 816 | _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b) |
| 817 | { |
| 818 | token_node_t *node_a, *node_b; |
| 819 | |
| 820 | node_a = a->head; |
| 821 | node_b = b->head; |
| 822 | |
| 823 | while (1) |
| 824 | { |
| 825 | if (node_a == NULL && node_b == NULL) |
| 826 | break; |
| 827 | |
| 828 | if (node_a == NULL || node_b == NULL) |
| 829 | return 0; |
| 830 | |
| 831 | if (node_a->token->type == SPACE) { |
| 832 | node_a = node_a->next; |
| 833 | continue; |
| 834 | } |
| 835 | |
| 836 | if (node_b->token->type == SPACE) { |
| 837 | node_b = node_b->next; |
| 838 | continue; |
| 839 | } |
| 840 | |
| 841 | if (node_a->token->type != node_b->token->type) |
| 842 | return 0; |
| 843 | |
| 844 | switch (node_a->token->type) { |
| 845 | case INTEGER: |
| 846 | if (node_a->token->value.ival != |
| 847 | node_b->token->value.ival) |
| 848 | { |
| 849 | return 0; |
| 850 | } |
| 851 | break; |
| 852 | case IDENTIFIER: |
| 853 | case INTEGER_STRING: |
| 854 | case OTHER: |
| 855 | if (strcmp (node_a->token->value.str, |
| 856 | node_b->token->value.str)) |
| 857 | { |
| 858 | return 0; |
| 859 | } |
| 860 | break; |
| 861 | } |
| 862 | |
| 863 | node_a = node_a->next; |
| 864 | node_b = node_b->next; |
| 865 | } |
| 866 | |
| 867 | return 1; |
| 868 | } |
| 869 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 870 | static void |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 871 | _token_print (char **out, token_t *token) |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 872 | { |
| 873 | if (token->type < 256) { |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 874 | glcpp_printf (*out, "%c", token->type); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 875 | return; |
| 876 | } |
| 877 | |
| 878 | switch (token->type) { |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 879 | case INTEGER: |
Eric Anholt | 8605c29 | 2010-07-28 16:53:51 -0700 | [diff] [blame] | 880 | glcpp_printf (*out, "%" PRIiMAX, token->value.ival); |
Carl Worth | 8fed1cd | 2010-05-26 09:32:12 -0700 | [diff] [blame] | 881 | break; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 882 | case IDENTIFIER: |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 883 | case INTEGER_STRING: |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 884 | case OTHER: |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 885 | glcpp_print (*out, token->value.str); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 886 | break; |
| 887 | case SPACE: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 888 | glcpp_print (*out, " "); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 889 | break; |
| 890 | case LEFT_SHIFT: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 891 | glcpp_print (*out, "<<"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 892 | break; |
| 893 | case RIGHT_SHIFT: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 894 | glcpp_print (*out, ">>"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 895 | break; |
| 896 | case LESS_OR_EQUAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 897 | glcpp_print (*out, "<="); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 898 | break; |
| 899 | case GREATER_OR_EQUAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 900 | glcpp_print (*out, ">="); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 901 | break; |
| 902 | case EQUAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 903 | glcpp_print (*out, "=="); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 904 | break; |
| 905 | case NOT_EQUAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 906 | glcpp_print (*out, "!="); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 907 | break; |
| 908 | case AND: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 909 | glcpp_print (*out, "&&"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 910 | break; |
| 911 | case OR: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 912 | glcpp_print (*out, "||"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 913 | break; |
| 914 | case PASTE: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 915 | glcpp_print (*out, "##"); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 916 | break; |
Carl Worth | dd74900 | 2010-05-27 10:12:33 -0700 | [diff] [blame] | 917 | case COMMA_FINAL: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 918 | glcpp_print (*out, ","); |
Carl Worth | dd74900 | 2010-05-27 10:12:33 -0700 | [diff] [blame] | 919 | break; |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 920 | case PLACEHOLDER: |
| 921 | /* Nothing to print. */ |
| 922 | break; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 923 | default: |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 924 | assert(!"Error: Don't know how to print token."); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 925 | break; |
| 926 | } |
| 927 | } |
| 928 | |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 929 | /* Return a new token (talloc()ed off of 'token') formed by pasting |
| 930 | * 'token' and 'other'. Note that this function may return 'token' or |
| 931 | * 'other' directly rather than allocating anything new. |
| 932 | * |
| 933 | * Caution: Only very cursory error-checking is performed to see if |
| 934 | * the final result is a valid single token. */ |
| 935 | static token_t * |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 936 | _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other) |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 937 | { |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 938 | token_t *combined = NULL; |
| 939 | |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 940 | /* Pasting a placeholder onto anything makes no change. */ |
| 941 | if (other->type == PLACEHOLDER) |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 942 | return token; |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 943 | |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 944 | /* When 'token' is a placeholder, just return 'other'. */ |
| 945 | if (token->type == PLACEHOLDER) |
| 946 | return other; |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 947 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 948 | /* A very few single-character punctuators can be combined |
| 949 | * with another to form a multi-character punctuator. */ |
| 950 | switch (token->type) { |
| 951 | case '<': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 952 | if (other->type == '<') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 953 | combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT); |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 954 | else if (other->type == '=') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 955 | combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 956 | break; |
| 957 | case '>': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 958 | if (other->type == '>') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 959 | combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT); |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 960 | else if (other->type == '=') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 961 | combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 962 | break; |
| 963 | case '=': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 964 | if (other->type == '=') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 965 | combined = _token_create_ival (token, EQUAL, EQUAL); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 966 | break; |
| 967 | case '!': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 968 | if (other->type == '=') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 969 | combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 970 | break; |
| 971 | case '&': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 972 | if (other->type == '&') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 973 | combined = _token_create_ival (token, AND, AND); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 974 | break; |
| 975 | case '|': |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 976 | if (other->type == '|') |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 977 | combined = _token_create_ival (token, OR, OR); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 978 | break; |
| 979 | } |
| 980 | |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 981 | if (combined != NULL) { |
| 982 | /* Inherit the location from the first token */ |
| 983 | combined->location = token->location; |
| 984 | return combined; |
| 985 | } |
| 986 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 987 | /* Two string-valued tokens can usually just be mashed |
| 988 | * together. |
| 989 | * |
Carl Worth | 050e3de | 2010-05-27 14:36:29 -0700 | [diff] [blame] | 990 | * XXX: This isn't actually legitimate. Several things here |
| 991 | * should result in a diagnostic since the result cannot be a |
| 992 | * valid, single pre-processing token. For example, pasting |
| 993 | * "123" and "abc" is not legal, but we don't catch that |
| 994 | * here. */ |
| 995 | if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) && |
| 996 | (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING)) |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 997 | { |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 998 | char *str; |
| 999 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 1000 | str = talloc_asprintf (token, "%s%s", token->value.str, |
| 1001 | other->value.str); |
Kenneth Graunke | b78c9dd | 2010-06-16 16:58:31 -0700 | [diff] [blame] | 1002 | combined = _token_create_str (token, token->type, str); |
| 1003 | combined->location = token->location; |
| 1004 | return combined; |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 1007 | glcpp_error (&token->location, parser, ""); |
Kenneth Graunke | 33eaa3e | 2010-06-18 19:52:36 -0700 | [diff] [blame] | 1008 | glcpp_print (parser->info_log, "Pasting \""); |
| 1009 | _token_print (&parser->info_log, token); |
| 1010 | glcpp_print (parser->info_log, "\" and \""); |
| 1011 | _token_print (&parser->info_log, other); |
| 1012 | glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n"); |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 1013 | |
| 1014 | return token; |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1017 | static void |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 1018 | _token_list_print (glcpp_parser_t *parser, token_list_t *list) |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1019 | { |
| 1020 | token_node_t *node; |
| 1021 | |
| 1022 | if (list == NULL) |
| 1023 | return; |
| 1024 | |
| 1025 | for (node = list->head; node; node = node->next) |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 1026 | _token_print (&parser->output, node->token); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 1029 | void |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 1030 | yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 1031 | { |
Kenneth Graunke | f1e6c06 | 2010-06-17 12:03:25 -0700 | [diff] [blame] | 1032 | glcpp_error(locp, parser, "%s", error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 1033 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 1034 | |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 1035 | static void add_builtin_define(glcpp_parser_t *parser, |
| 1036 | const char *name, int value) |
| 1037 | { |
| 1038 | token_t *tok; |
| 1039 | token_list_t *list; |
| 1040 | |
| 1041 | tok = _token_create_ival (parser, INTEGER, value); |
| 1042 | |
| 1043 | list = _token_list_create(parser); |
| 1044 | _token_list_append(list, tok); |
| 1045 | _define_object_macro(parser, NULL, name, list); |
| 1046 | |
| 1047 | talloc_unlink(parser, tok); |
| 1048 | } |
| 1049 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 1050 | glcpp_parser_t * |
Ian Romanick | 06143ea | 2010-06-30 16:27:22 -0700 | [diff] [blame] | 1051 | glcpp_parser_create (const struct gl_extensions *extensions) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 1052 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 1053 | glcpp_parser_t *parser; |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 1054 | int language_version; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 1055 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 1056 | parser = talloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 1057 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1058 | glcpp_lex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 1059 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 1060 | hash_table_string_compare); |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1061 | parser->active = NULL; |
Carl Worth | a771a40 | 2010-06-01 11:20:18 -0700 | [diff] [blame] | 1062 | parser->lexing_if = 0; |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 1063 | parser->space_tokens = 1; |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 1064 | parser->newline_as_space = 0; |
| 1065 | parser->in_control_line = 0; |
| 1066 | parser->paren_count = 0; |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame] | 1067 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1068 | parser->skip_stack = NULL; |
| 1069 | |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 1070 | parser->lex_from_list = NULL; |
| 1071 | parser->lex_from_node = NULL; |
| 1072 | |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 1073 | parser->output = talloc_strdup(parser, ""); |
Kenneth Graunke | 33eaa3e | 2010-06-18 19:52:36 -0700 | [diff] [blame] | 1074 | parser->info_log = talloc_strdup(parser, ""); |
Kenneth Graunke | 1b85c46 | 2010-06-21 13:55:12 -0700 | [diff] [blame] | 1075 | parser->error = 0; |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 1076 | |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 1077 | /* Add pre-defined macros. */ |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 1078 | add_builtin_define(parser, "GL_ARB_draw_buffers", 1); |
| 1079 | add_builtin_define(parser, "GL_ARB_texture_rectangle", 1); |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 1080 | |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 1081 | if (extensions != NULL) { |
| 1082 | if (extensions->EXT_texture_array) { |
| 1083 | add_builtin_define(parser, "GL_EXT_texture_array", 1); |
| 1084 | } |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 1085 | |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 1086 | if (extensions->ARB_fragment_coord_conventions) |
| 1087 | add_builtin_define(parser, "GL_ARB_fragment_coord_conventions", |
| 1088 | 1); |
Ian Romanick | 06143ea | 2010-06-30 16:27:22 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 1091 | language_version = 110; |
Eric Anholt | d4a04f3 | 2010-07-28 16:58:39 -0700 | [diff] [blame] | 1092 | add_builtin_define(parser, "__VERSION__", language_version); |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 1093 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 1094 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | int |
| 1098 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 1099 | { |
| 1100 | return yyparse (parser); |
| 1101 | } |
| 1102 | |
| 1103 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 1104 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 1105 | { |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1106 | glcpp_lex_destroy (parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 1107 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 1108 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 1109 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 1110 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1111 | typedef enum function_status |
| 1112 | { |
| 1113 | FUNCTION_STATUS_SUCCESS, |
| 1114 | FUNCTION_NOT_A_FUNCTION, |
| 1115 | FUNCTION_UNBALANCED_PARENTHESES |
| 1116 | } function_status_t; |
| 1117 | |
| 1118 | /* Find a set of function-like macro arguments by looking for a |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1119 | * balanced set of parentheses. |
| 1120 | * |
| 1121 | * When called, 'node' should be the opening-parenthesis token, (or |
| 1122 | * perhaps preceeding SPACE tokens). Upon successful return *last will |
| 1123 | * be the last consumed node, (corresponding to the closing right |
| 1124 | * parenthesis). |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1125 | * |
| 1126 | * Return values: |
| 1127 | * |
| 1128 | * FUNCTION_STATUS_SUCCESS: |
| 1129 | * |
| 1130 | * Successfully parsed a set of function arguments. |
| 1131 | * |
| 1132 | * FUNCTION_NOT_A_FUNCTION: |
| 1133 | * |
| 1134 | * Macro name not followed by a '('. This is not an error, but |
| 1135 | * simply that the macro name should be treated as a non-macro. |
| 1136 | * |
Carl Worth | 14c98a5 | 2010-06-02 15:49:54 -0700 | [diff] [blame] | 1137 | * FUNCTION_UNBALANCED_PARENTHESES |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1138 | * |
| 1139 | * Macro name is not followed by a balanced set of parentheses. |
| 1140 | */ |
| 1141 | static function_status_t |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1142 | _arguments_parse (argument_list_t *arguments, |
| 1143 | token_node_t *node, |
| 1144 | token_node_t **last) |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1145 | { |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1146 | token_list_t *argument; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1147 | int paren_count; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1148 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1149 | node = node->next; |
| 1150 | |
| 1151 | /* Ignore whitespace before first parenthesis. */ |
| 1152 | while (node && node->token->type == SPACE) |
| 1153 | node = node->next; |
| 1154 | |
| 1155 | if (node == NULL || node->token->type != '(') |
| 1156 | return FUNCTION_NOT_A_FUNCTION; |
| 1157 | |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 1158 | node = node->next; |
| 1159 | |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame] | 1160 | argument = _token_list_create (arguments); |
| 1161 | _argument_list_append (arguments, argument); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1162 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1163 | for (paren_count = 1; node; node = node->next) { |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1164 | if (node->token->type == '(') |
| 1165 | { |
| 1166 | paren_count++; |
| 1167 | } |
| 1168 | else if (node->token->type == ')') |
| 1169 | { |
| 1170 | paren_count--; |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1171 | if (paren_count == 0) |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 1172 | break; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1173 | } |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 1174 | |
| 1175 | if (node->token->type == ',' && |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1176 | paren_count == 1) |
| 1177 | { |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame] | 1178 | _token_list_trim_trailing_space (argument); |
| 1179 | argument = _token_list_create (arguments); |
| 1180 | _argument_list_append (arguments, argument); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1181 | } |
| 1182 | else { |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame] | 1183 | if (argument->head == NULL) { |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 1184 | /* Don't treat initial whitespace as |
| 1185 | * part of the arguement. */ |
| 1186 | if (node->token->type == SPACE) |
| 1187 | continue; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1188 | } |
| 1189 | _token_list_append (argument, node->token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1190 | } |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 1191 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1192 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1193 | if (paren_count) |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1194 | return FUNCTION_UNBALANCED_PARENTHESES; |
| 1195 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1196 | *last = node; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1197 | |
| 1198 | return FUNCTION_STATUS_SUCCESS; |
| 1199 | } |
| 1200 | |
Carl Worth | e1acbfc | 2010-07-20 16:44:03 -0700 | [diff] [blame] | 1201 | static token_list_t * |
| 1202 | _token_list_create_with_one_space (void *ctx) |
| 1203 | { |
| 1204 | token_list_t *list; |
| 1205 | token_t *space; |
| 1206 | |
| 1207 | list = _token_list_create (ctx); |
| 1208 | space = _token_create_ival (list, SPACE, SPACE); |
| 1209 | _token_list_append (list, space); |
| 1210 | |
| 1211 | return list; |
| 1212 | } |
| 1213 | |
Kenneth Graunke | 16b4eed | 2010-08-04 16:10:03 -0700 | [diff] [blame] | 1214 | static void |
| 1215 | _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list) |
| 1216 | { |
| 1217 | token_list_t *expanded; |
| 1218 | token_t *token; |
| 1219 | |
| 1220 | expanded = _token_list_create (parser); |
| 1221 | token = _token_create_ival (parser, type, type); |
| 1222 | _token_list_append (expanded, token); |
| 1223 | _glcpp_parser_expand_token_list (parser, list); |
| 1224 | _token_list_append_list (expanded, list); |
| 1225 | glcpp_parser_lex_from (parser, expanded); |
| 1226 | } |
| 1227 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1228 | /* This is a helper function that's essentially part of the |
| 1229 | * implementation of _glcpp_parser_expand_node. It shouldn't be called |
| 1230 | * except for by that function. |
| 1231 | * |
| 1232 | * Returns NULL if node is a simple token with no expansion, (that is, |
| 1233 | * although 'node' corresponds to an identifier defined as a |
| 1234 | * function-like macro, it is not followed with a parenthesized |
| 1235 | * argument list). |
| 1236 | * |
| 1237 | * Compute the complete expansion of node (which is a function-like |
| 1238 | * macro) and subsequent nodes which are arguments. |
| 1239 | * |
| 1240 | * Returns the token list that results from the expansion and sets |
| 1241 | * *last to the last node in the list that was consumed by the |
Carl Worth | fbe4240 | 2010-07-22 16:36:04 -0700 | [diff] [blame] | 1242 | * expansion. Specifically, *last will be set as follows: as the |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1243 | * token of the closing right parenthesis. |
| 1244 | */ |
| 1245 | static token_list_t * |
| 1246 | _glcpp_parser_expand_function (glcpp_parser_t *parser, |
| 1247 | token_node_t *node, |
| 1248 | token_node_t **last) |
| 1249 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1250 | { |
| 1251 | macro_t *macro; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1252 | const char *identifier; |
| 1253 | argument_list_t *arguments; |
| 1254 | function_status_t status; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1255 | token_list_t *substituted; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1256 | int parameter_index; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1257 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1258 | identifier = node->token->value.str; |
| 1259 | |
| 1260 | macro = hash_table_find (parser->defines, identifier); |
| 1261 | |
| 1262 | assert (macro->is_function); |
| 1263 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1264 | arguments = _argument_list_create (parser); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1265 | status = _arguments_parse (arguments, node, last); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1266 | |
| 1267 | switch (status) { |
| 1268 | case FUNCTION_STATUS_SUCCESS: |
| 1269 | break; |
| 1270 | case FUNCTION_NOT_A_FUNCTION: |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1271 | return NULL; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 1272 | case FUNCTION_UNBALANCED_PARENTHESES: |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 1273 | glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1274 | return NULL; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
Carl Worth | e1acbfc | 2010-07-20 16:44:03 -0700 | [diff] [blame] | 1277 | /* Replace a macro defined as empty with a SPACE token. */ |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1278 | if (macro->replacements == NULL) { |
| 1279 | talloc_free (arguments); |
Carl Worth | e1acbfc | 2010-07-20 16:44:03 -0700 | [diff] [blame] | 1280 | return _token_list_create_with_one_space (parser); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
Carl Worth | a19297b | 2010-05-27 13:29:19 -0700 | [diff] [blame] | 1283 | if (! ((_argument_list_length (arguments) == |
| 1284 | _string_list_length (macro->parameters)) || |
| 1285 | (_string_list_length (macro->parameters) == 0 && |
| 1286 | _argument_list_length (arguments) == 1 && |
| 1287 | arguments->head->argument->head == NULL))) |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1288 | { |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 1289 | glcpp_error (&node->token->location, parser, |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 1290 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 1291 | identifier, |
| 1292 | _argument_list_length (arguments), |
| 1293 | _string_list_length (macro->parameters)); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1294 | return NULL; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1297 | /* Perform argument substitution on the replacement list. */ |
| 1298 | substituted = _token_list_create (arguments); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1299 | |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame] | 1300 | for (node = macro->replacements->head; node; node = node->next) |
| 1301 | { |
| 1302 | if (node->token->type == IDENTIFIER && |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1303 | _string_list_contains (macro->parameters, |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame] | 1304 | node->token->value.str, |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1305 | ¶meter_index)) |
| 1306 | { |
| 1307 | token_list_t *argument; |
| 1308 | argument = _argument_list_member_at (arguments, |
| 1309 | parameter_index); |
Carl Worth | d5cd403 | 2010-05-26 08:09:29 -0700 | [diff] [blame] | 1310 | /* Before substituting, we expand the argument |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 1311 | * tokens, or append a placeholder token for |
| 1312 | * an empty argument. */ |
| 1313 | if (argument->head) { |
Carl Worth | fbe4240 | 2010-07-22 16:36:04 -0700 | [diff] [blame] | 1314 | token_list_t *expanded_argument; |
| 1315 | expanded_argument = _token_list_copy (parser, |
| 1316 | argument); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1317 | _glcpp_parser_expand_token_list (parser, |
Carl Worth | fbe4240 | 2010-07-22 16:36:04 -0700 | [diff] [blame] | 1318 | expanded_argument); |
| 1319 | _token_list_append_list (substituted, |
| 1320 | expanded_argument); |
Carl Worth | 85b50e8 | 2010-05-27 14:01:18 -0700 | [diff] [blame] | 1321 | } else { |
| 1322 | token_t *new_token; |
| 1323 | |
| 1324 | new_token = _token_create_ival (substituted, |
| 1325 | PLACEHOLDER, |
| 1326 | PLACEHOLDER); |
| 1327 | _token_list_append (substituted, new_token); |
| 1328 | } |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1329 | } else { |
Carl Worth | ce540f2 | 2010-05-26 08:25:44 -0700 | [diff] [blame] | 1330 | _token_list_append (substituted, node->token); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1334 | /* After argument substitution, and before further expansion |
| 1335 | * below, implement token pasting. */ |
| 1336 | |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 1337 | _token_list_trim_trailing_space (substituted); |
| 1338 | |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1339 | node = substituted->head; |
| 1340 | while (node) |
| 1341 | { |
| 1342 | token_node_t *next_non_space; |
| 1343 | |
| 1344 | /* Look ahead for a PASTE token, skipping space. */ |
| 1345 | next_non_space = node->next; |
| 1346 | while (next_non_space && next_non_space->token->type == SPACE) |
| 1347 | next_non_space = next_non_space->next; |
| 1348 | |
| 1349 | if (next_non_space == NULL) |
| 1350 | break; |
| 1351 | |
| 1352 | if (next_non_space->token->type != PASTE) { |
| 1353 | node = next_non_space; |
| 1354 | continue; |
| 1355 | } |
| 1356 | |
| 1357 | /* Now find the next non-space token after the PASTE. */ |
| 1358 | next_non_space = next_non_space->next; |
| 1359 | while (next_non_space && next_non_space->token->type == SPACE) |
| 1360 | next_non_space = next_non_space->next; |
| 1361 | |
| 1362 | if (next_non_space == NULL) { |
Kenneth Graunke | ca9e5fc | 2010-06-16 17:31:50 -0700 | [diff] [blame] | 1363 | yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n"); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1364 | return NULL; |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 1367 | node->token = _token_paste (parser, node->token, next_non_space->token); |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1368 | node->next = next_non_space->next; |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 1369 | if (next_non_space == substituted->tail) |
| 1370 | substituted->tail = node; |
Carl Worth | ad0dee6 | 2010-05-26 09:04:50 -0700 | [diff] [blame] | 1371 | |
| 1372 | node = node->next; |
| 1373 | } |
| 1374 | |
Carl Worth | b06096e | 2010-05-29 05:54:19 -0700 | [diff] [blame] | 1375 | substituted->non_space_tail = substituted->tail; |
| 1376 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1377 | return substituted; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1380 | /* Compute the complete expansion of node, (and subsequent nodes after |
| 1381 | * 'node' in the case that 'node' is a function-like macro and |
| 1382 | * subsequent nodes are arguments). |
| 1383 | * |
| 1384 | * Returns NULL if node is a simple token with no expansion. |
| 1385 | * |
| 1386 | * Otherwise, returns the token list that results from the expansion |
| 1387 | * and sets *last to the last node in the list that was consumed by |
Carl Worth | e1acbfc | 2010-07-20 16:44:03 -0700 | [diff] [blame] | 1388 | * the expansion. Specifically, *last will be set as follows: |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1389 | * |
| 1390 | * As 'node' in the case of object-like macro expansion. |
| 1391 | * |
| 1392 | * As the token of the closing right parenthesis in the case of |
| 1393 | * function-like macro expansion. |
| 1394 | */ |
| 1395 | static token_list_t * |
| 1396 | _glcpp_parser_expand_node (glcpp_parser_t *parser, |
| 1397 | token_node_t *node, |
| 1398 | token_node_t **last) |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1399 | { |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1400 | token_t *token = node->token; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1401 | const char *identifier; |
| 1402 | macro_t *macro; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1403 | |
| 1404 | /* We only expand identifiers */ |
| 1405 | if (token->type != IDENTIFIER) { |
| 1406 | /* We change any COMMA into a COMMA_FINAL to prevent |
| 1407 | * it being mistaken for an argument separator |
| 1408 | * later. */ |
| 1409 | if (token->type == ',') { |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1410 | token->type = COMMA_FINAL; |
| 1411 | token->value.ival = COMMA_FINAL; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1412 | } |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1413 | |
| 1414 | return NULL; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | /* Look up this identifier in the hash table. */ |
| 1418 | identifier = token->value.str; |
| 1419 | macro = hash_table_find (parser->defines, identifier); |
| 1420 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1421 | /* Not a macro, so no expansion needed. */ |
| 1422 | if (macro == NULL) |
| 1423 | return NULL; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1424 | |
| 1425 | /* Finally, don't expand this macro if we're already actively |
| 1426 | * expanding it, (to avoid infinite recursion). */ |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1427 | if (_active_list_contains (parser->active, identifier)) { |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1428 | /* We change the token type here from IDENTIFIER to |
| 1429 | * OTHER to prevent any future expansion of this |
| 1430 | * unexpanded token. */ |
| 1431 | char *str; |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1432 | token_list_t *expansion; |
| 1433 | token_t *final; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1434 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 1435 | str = talloc_strdup (parser, token->value.str); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1436 | final = _token_create_str (parser, OTHER, str); |
| 1437 | expansion = _token_list_create (parser); |
| 1438 | _token_list_append (expansion, final); |
| 1439 | *last = node; |
| 1440 | return expansion; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1443 | if (! macro->is_function) |
| 1444 | { |
| 1445 | *last = node; |
| 1446 | |
Carl Worth | e1acbfc | 2010-07-20 16:44:03 -0700 | [diff] [blame] | 1447 | /* Replace a macro defined as empty with a SPACE token. */ |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1448 | if (macro->replacements == NULL) |
Carl Worth | e1acbfc | 2010-07-20 16:44:03 -0700 | [diff] [blame] | 1449 | return _token_list_create_with_one_space (parser); |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1450 | |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1451 | return _token_list_copy (parser, macro->replacements); |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1452 | } |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1453 | |
| 1454 | return _glcpp_parser_expand_function (parser, node, last); |
| 1455 | } |
| 1456 | |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1457 | /* Push a new identifier onto the active list, returning the new list. |
| 1458 | * |
| 1459 | * Here, 'marker' is the token node that appears in the list after the |
| 1460 | * expansion of 'identifier'. That is, when the list iterator begins |
| 1461 | * examinging 'marker', then it is time to pop this node from the |
| 1462 | * active stack. |
| 1463 | */ |
| 1464 | active_list_t * |
| 1465 | _active_list_push (active_list_t *list, |
| 1466 | const char *identifier, |
| 1467 | token_node_t *marker) |
| 1468 | { |
| 1469 | active_list_t *node; |
| 1470 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 1471 | node = talloc (list, active_list_t); |
| 1472 | node->identifier = talloc_strdup (node, identifier); |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1473 | node->marker = marker; |
| 1474 | node->next = list; |
| 1475 | |
| 1476 | return node; |
| 1477 | } |
| 1478 | |
| 1479 | active_list_t * |
| 1480 | _active_list_pop (active_list_t *list) |
| 1481 | { |
| 1482 | active_list_t *node = list; |
| 1483 | |
| 1484 | if (node == NULL) |
| 1485 | return NULL; |
| 1486 | |
| 1487 | node = list->next; |
| 1488 | talloc_free (list); |
| 1489 | |
| 1490 | return node; |
| 1491 | } |
| 1492 | |
| 1493 | int |
| 1494 | _active_list_contains (active_list_t *list, const char *identifier) |
| 1495 | { |
| 1496 | active_list_t *node; |
| 1497 | |
| 1498 | if (list == NULL) |
| 1499 | return 0; |
| 1500 | |
| 1501 | for (node = list; node; node = node->next) |
| 1502 | if (strcmp (node->identifier, identifier) == 0) |
| 1503 | return 1; |
| 1504 | |
| 1505 | return 0; |
| 1506 | } |
| 1507 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1508 | /* Walk over the token list replacing nodes with their expansion. |
| 1509 | * Whenever nodes are expanded the walking will walk over the new |
| 1510 | * nodes, continuing to expand as necessary. The results are placed in |
| 1511 | * 'list' itself; |
| 1512 | */ |
| 1513 | static void |
| 1514 | _glcpp_parser_expand_token_list (glcpp_parser_t *parser, |
| 1515 | token_list_t *list) |
| 1516 | { |
| 1517 | token_node_t *node_prev; |
Carl Worth | c42e640 | 2010-06-22 15:51:34 -0700 | [diff] [blame] | 1518 | token_node_t *node, *last = NULL; |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1519 | token_list_t *expansion; |
| 1520 | |
| 1521 | if (list == NULL) |
| 1522 | return; |
| 1523 | |
| 1524 | _token_list_trim_trailing_space (list); |
| 1525 | |
| 1526 | node_prev = NULL; |
| 1527 | node = list->head; |
| 1528 | |
| 1529 | while (node) { |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1530 | |
| 1531 | while (parser->active && parser->active->marker == node) |
| 1532 | parser->active = _active_list_pop (parser->active); |
| 1533 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1534 | /* Find the expansion for node, which will replace all |
| 1535 | * nodes from node to last, inclusive. */ |
| 1536 | expansion = _glcpp_parser_expand_node (parser, node, &last); |
| 1537 | if (expansion) { |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1538 | token_node_t *n; |
| 1539 | |
| 1540 | for (n = node; n != last->next; n = n->next) |
| 1541 | while (parser->active && |
| 1542 | parser->active->marker == n) |
| 1543 | { |
| 1544 | parser->active = _active_list_pop (parser->active); |
| 1545 | } |
| 1546 | |
| 1547 | parser->active = _active_list_push (parser->active, |
| 1548 | node->token->value.str, |
| 1549 | last->next); |
| 1550 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1551 | /* Splice expansion into list, supporting a |
| 1552 | * simple deletion if the expansion is |
| 1553 | * empty. */ |
| 1554 | if (expansion->head) { |
| 1555 | if (node_prev) |
| 1556 | node_prev->next = expansion->head; |
| 1557 | else |
| 1558 | list->head = expansion->head; |
| 1559 | expansion->tail->next = last->next; |
| 1560 | if (last == list->tail) |
| 1561 | list->tail = expansion->tail; |
| 1562 | } else { |
| 1563 | if (node_prev) |
| 1564 | node_prev->next = last->next; |
| 1565 | else |
| 1566 | list->head = last->next; |
| 1567 | if (last == list->tail) |
Kenneth Graunke | 0656f6b | 2010-06-16 11:56:36 -0700 | [diff] [blame] | 1568 | list->tail = NULL; |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1569 | } |
| 1570 | } else { |
| 1571 | node_prev = node; |
| 1572 | } |
| 1573 | node = node_prev ? node_prev->next : list->head; |
| 1574 | } |
| 1575 | |
Carl Worth | 22b3ace | 2010-06-02 15:32:03 -0700 | [diff] [blame] | 1576 | while (parser->active) |
| 1577 | parser->active = _active_list_pop (parser->active); |
| 1578 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1579 | list->non_space_tail = list->tail; |
Carl Worth | 3c93d39 | 2010-05-28 08:17:46 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1582 | void |
| 1583 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 1584 | token_list_t *list) |
| 1585 | { |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1586 | if (list == NULL) |
| 1587 | return; |
| 1588 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1589 | _glcpp_parser_expand_token_list (parser, list); |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 1590 | |
Carl Worth | 681afbc | 2010-05-28 15:06:02 -0700 | [diff] [blame] | 1591 | _token_list_trim_trailing_space (list); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 1592 | |
Kenneth Graunke | 4c8a1af | 2010-06-16 11:57:48 -0700 | [diff] [blame] | 1593 | _token_list_print (parser, list); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 1594 | } |
| 1595 | |
Carl Worth | d80dcaf | 2010-07-20 15:55:21 -0700 | [diff] [blame] | 1596 | static void |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1597 | _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc, |
| 1598 | const char *identifier) |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1599 | { |
| 1600 | /* According to the GLSL specification, macro names starting with "__" |
| 1601 | * or "GL_" are reserved for future use. So, don't allow them. |
| 1602 | */ |
| 1603 | if (strncmp(identifier, "__", 2) == 0) { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1604 | glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n"); |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1605 | } |
| 1606 | if (strncmp(identifier, "GL_", 3) == 0) { |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1607 | glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n"); |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1608 | } |
| 1609 | } |
| 1610 | |
Carl Worth | 3882cf2 | 2010-08-17 23:20:58 -0700 | [diff] [blame] | 1611 | static int |
| 1612 | _macro_equal (macro_t *a, macro_t *b) |
| 1613 | { |
| 1614 | if (a->is_function != b->is_function) |
| 1615 | return 0; |
| 1616 | |
| 1617 | if (a->is_function) { |
| 1618 | if (! _string_list_equal (a->parameters, b->parameters)) |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
| 1622 | return _token_list_equal_ignoring_space (a->replacements, |
| 1623 | b->replacements); |
| 1624 | } |
| 1625 | |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1626 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1627 | _define_object_macro (glcpp_parser_t *parser, |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1628 | YYLTYPE *loc, |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1629 | const char *identifier, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 1630 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1631 | { |
Carl Worth | 3882cf2 | 2010-08-17 23:20:58 -0700 | [diff] [blame] | 1632 | macro_t *macro, *previous; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1633 | |
Ian Romanick | 2d12236 | 2010-06-30 16:03:19 -0700 | [diff] [blame] | 1634 | if (loc != NULL) |
| 1635 | _check_for_reserved_macro_name(parser, loc, identifier); |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1636 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 1637 | macro = talloc (parser, macro_t); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1638 | |
| 1639 | macro->is_function = 0; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 1640 | macro->parameters = NULL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 1641 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1642 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1643 | |
Carl Worth | 3882cf2 | 2010-08-17 23:20:58 -0700 | [diff] [blame] | 1644 | previous = hash_table_find (parser->defines, identifier); |
| 1645 | if (previous) { |
| 1646 | if (_macro_equal (macro, previous)) { |
| 1647 | talloc_free (macro); |
| 1648 | return; |
| 1649 | } |
| 1650 | glcpp_error (loc, parser, "Redefinition of macro %s\n", |
| 1651 | identifier); |
| 1652 | } |
| 1653 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1654 | hash_table_insert (parser->defines, macro, identifier); |
| 1655 | } |
| 1656 | |
| 1657 | void |
| 1658 | _define_function_macro (glcpp_parser_t *parser, |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1659 | YYLTYPE *loc, |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1660 | const char *identifier, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 1661 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 1662 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1663 | { |
Carl Worth | 3882cf2 | 2010-08-17 23:20:58 -0700 | [diff] [blame] | 1664 | macro_t *macro, *previous; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1665 | |
Kenneth Graunke | dcdf62f | 2010-06-17 12:21:53 -0700 | [diff] [blame] | 1666 | _check_for_reserved_macro_name(parser, loc, identifier); |
Kenneth Graunke | 2ab0b13 | 2010-06-04 14:53:58 -0700 | [diff] [blame] | 1667 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 1668 | macro = talloc (parser, macro_t); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1669 | |
| 1670 | macro->is_function = 1; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 1671 | macro->parameters = talloc_steal (macro, parameters); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 1672 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 1673 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1674 | |
Carl Worth | 3882cf2 | 2010-08-17 23:20:58 -0700 | [diff] [blame] | 1675 | previous = hash_table_find (parser->defines, identifier); |
| 1676 | if (previous) { |
| 1677 | if (_macro_equal (macro, previous)) { |
| 1678 | talloc_free (macro); |
| 1679 | return; |
| 1680 | } |
| 1681 | glcpp_error (loc, parser, "Redefinition of macro %s\n", |
| 1682 | identifier); |
| 1683 | } |
| 1684 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 1685 | hash_table_insert (parser->defines, macro, identifier); |
| 1686 | } |
| 1687 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1688 | static int |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 1689 | glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser) |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1690 | { |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 1691 | token_node_t *node; |
| 1692 | int ret; |
| 1693 | |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 1694 | if (parser->lex_from_list == NULL) { |
Kenneth Graunke | 465e03e | 2010-06-16 16:35:57 -0700 | [diff] [blame] | 1695 | ret = glcpp_lex (yylval, yylloc, parser->scanner); |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 1696 | |
| 1697 | /* XXX: This ugly block of code exists for the sole |
| 1698 | * purpose of converting a NEWLINE token into a SPACE |
| 1699 | * token, but only in the case where we have seen a |
| 1700 | * function-like macro name, but have not yet seen its |
| 1701 | * closing parenthesis. |
| 1702 | * |
| 1703 | * There's perhaps a more compact way to do this with |
| 1704 | * mid-rule actions in the grammar. |
| 1705 | * |
| 1706 | * I'm definitely not pleased with the complexity of |
| 1707 | * this code here. |
| 1708 | */ |
| 1709 | if (parser->newline_as_space) |
| 1710 | { |
| 1711 | if (ret == '(') { |
| 1712 | parser->paren_count++; |
| 1713 | } else if (ret == ')') { |
| 1714 | parser->paren_count--; |
| 1715 | if (parser->paren_count == 0) |
| 1716 | parser->newline_as_space = 0; |
| 1717 | } else if (ret == NEWLINE) { |
| 1718 | ret = SPACE; |
| 1719 | } else if (ret != SPACE) { |
| 1720 | if (parser->paren_count == 0) |
| 1721 | parser->newline_as_space = 0; |
| 1722 | } |
| 1723 | } |
| 1724 | else if (parser->in_control_line) |
| 1725 | { |
| 1726 | if (ret == NEWLINE) |
| 1727 | parser->in_control_line = 0; |
| 1728 | } |
| 1729 | else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC || |
| 1730 | ret == HASH_UNDEF || ret == HASH_IF || |
| 1731 | ret == HASH_IFDEF || ret == HASH_IFNDEF || |
| 1732 | ret == HASH_ELIF || ret == HASH_ELSE || |
| 1733 | ret == HASH_ENDIF || ret == HASH) |
| 1734 | { |
| 1735 | parser->in_control_line = 1; |
| 1736 | } |
| 1737 | else if (ret == IDENTIFIER) |
| 1738 | { |
| 1739 | macro_t *macro; |
| 1740 | macro = hash_table_find (parser->defines, |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 1741 | yylval->str); |
Carl Worth | 95951ea | 2010-05-26 15:57:10 -0700 | [diff] [blame] | 1742 | if (macro && macro->is_function) { |
| 1743 | parser->newline_as_space = 1; |
| 1744 | parser->paren_count = 0; |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | return ret; |
| 1749 | } |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 1750 | |
| 1751 | node = parser->lex_from_node; |
| 1752 | |
| 1753 | if (node == NULL) { |
| 1754 | talloc_free (parser->lex_from_list); |
| 1755 | parser->lex_from_list = NULL; |
| 1756 | return NEWLINE; |
| 1757 | } |
| 1758 | |
Kenneth Graunke | e0e429f | 2010-06-16 16:26:28 -0700 | [diff] [blame] | 1759 | *yylval = node->token->value; |
Carl Worth | 8e82fcb | 2010-05-26 11:15:21 -0700 | [diff] [blame] | 1760 | ret = node->token->type; |
| 1761 | |
| 1762 | parser->lex_from_node = node->next; |
| 1763 | |
| 1764 | return ret; |
| 1765 | } |
| 1766 | |
| 1767 | static void |
| 1768 | glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list) |
| 1769 | { |
| 1770 | token_node_t *node; |
| 1771 | |
| 1772 | assert (parser->lex_from_list == NULL); |
| 1773 | |
| 1774 | /* Copy list, eliminating any space tokens. */ |
| 1775 | parser->lex_from_list = _token_list_create (parser); |
| 1776 | |
| 1777 | for (node = list->head; node; node = node->next) { |
| 1778 | if (node->token->type == SPACE) |
| 1779 | continue; |
| 1780 | _token_list_append (parser->lex_from_list, node->token); |
| 1781 | } |
| 1782 | |
| 1783 | talloc_free (list); |
| 1784 | |
| 1785 | parser->lex_from_node = parser->lex_from_list->head; |
| 1786 | |
| 1787 | /* It's possible the list consisted of nothing but whitespace. */ |
| 1788 | if (parser->lex_from_node == NULL) { |
| 1789 | talloc_free (parser->lex_from_list); |
| 1790 | parser->lex_from_list = NULL; |
| 1791 | } |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 1792 | } |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1793 | |
| 1794 | static void |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 1795 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc, |
| 1796 | int condition) |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1797 | { |
| 1798 | skip_type_t current = SKIP_NO_SKIP; |
| 1799 | skip_node_t *node; |
| 1800 | |
| 1801 | if (parser->skip_stack) |
| 1802 | current = parser->skip_stack->type; |
| 1803 | |
Kenneth Graunke | 1ffc1cd | 2010-08-03 20:21:52 -0700 | [diff] [blame] | 1804 | node = talloc (parser, skip_node_t); |
Kenneth Graunke | 0774523 | 2010-06-17 12:41:46 -0700 | [diff] [blame] | 1805 | node->loc = *loc; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1806 | |
| 1807 | if (current == SKIP_NO_SKIP) { |
| 1808 | if (condition) |
| 1809 | node->type = SKIP_NO_SKIP; |
| 1810 | else |
| 1811 | node->type = SKIP_TO_ELSE; |
| 1812 | } else { |
| 1813 | node->type = SKIP_TO_ENDIF; |
| 1814 | } |
| 1815 | |
| 1816 | node->next = parser->skip_stack; |
| 1817 | parser->skip_stack = node; |
| 1818 | } |
| 1819 | |
| 1820 | static void |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 1821 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc, |
| 1822 | const char *type, int condition) |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1823 | { |
| 1824 | if (parser->skip_stack == NULL) { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 1825 | glcpp_error (loc, parser, "%s without #if\n", type); |
Kenneth Graunke | e8e93a4 | 2010-06-17 12:58:54 -0700 | [diff] [blame] | 1826 | return; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | if (parser->skip_stack->type == SKIP_TO_ELSE) { |
| 1830 | if (condition) |
| 1831 | parser->skip_stack->type = SKIP_NO_SKIP; |
| 1832 | } else { |
| 1833 | parser->skip_stack->type = SKIP_TO_ENDIF; |
| 1834 | } |
| 1835 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 1836 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1837 | static void |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 1838 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc) |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1839 | { |
| 1840 | skip_node_t *node; |
| 1841 | |
| 1842 | if (parser->skip_stack == NULL) { |
Kenneth Graunke | 8a132aa | 2010-06-17 12:30:57 -0700 | [diff] [blame] | 1843 | glcpp_error (loc, parser, "#endif without #if\n"); |
Kenneth Graunke | e8e93a4 | 2010-06-17 12:58:54 -0700 | [diff] [blame] | 1844 | return; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | node = parser->skip_stack; |
| 1848 | parser->skip_stack = node->next; |
| 1849 | talloc_free (node); |
| 1850 | } |