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