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