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