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