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 | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 28 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 29 | #include "glcpp.h" |
| 30 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 31 | static void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 32 | yyerror (void *scanner, const char *error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 33 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 34 | static void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 35 | _define_object_macro (glcpp_parser_t *parser, |
| 36 | const char *macro, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 37 | token_list_t *replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -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_function_macro (glcpp_parser_t *parser, |
| 41 | const char *macro, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 42 | string_list_t *parameters, |
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 string_list_t * |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 46 | _string_list_create (void *ctx); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 47 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 48 | static void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 49 | _string_list_append_item (string_list_t *list, const char *str); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 50 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 51 | static void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 52 | _string_list_append_list (string_list_t *list, string_list_t *tail); |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 53 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 54 | static void |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 55 | _string_list_push (string_list_t *list, const char *str); |
| 56 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 57 | static void |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 58 | _string_list_pop (string_list_t *list); |
| 59 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 60 | static int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 61 | _string_list_contains (string_list_t *list, const char *member, int *index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 62 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 63 | static int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 64 | _string_list_length (string_list_t *list); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 65 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 66 | static argument_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 67 | _argument_list_create (void *ctx); |
| 68 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 69 | static void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 70 | _argument_list_append (argument_list_t *list, token_list_t *argument); |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 71 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 72 | static int |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 73 | _argument_list_length (argument_list_t *list); |
| 74 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 75 | static token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 76 | _argument_list_member_at (argument_list_t *list, int index); |
| 77 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 78 | /* Note: This function talloc_steal()s the str pointer. */ |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 79 | static token_t * |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 80 | _token_create_str (void *ctx, int type, char *str); |
| 81 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 82 | static token_t * |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 83 | _token_create_ival (void *ctx, int type, int ival); |
| 84 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 85 | static token_list_t * |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 86 | _token_list_create (void *ctx); |
| 87 | |
Carl Worth | b1ae61a | 2010-05-26 08:10:38 -0700 | [diff] [blame] | 88 | /* Note: This function adds a talloc_reference() to token. |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 89 | * |
| 90 | * You may want to talloc_unlink any current reference if you no |
| 91 | * longer need it. */ |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 92 | static void |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 93 | _token_list_append (token_list_t *list, token_t *token); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 94 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 95 | static void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 96 | _token_list_append_list (token_list_t *list, token_list_t *tail); |
| 97 | |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 98 | static void |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 99 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 100 | token_list_t *list); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 101 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 102 | static void |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 103 | _glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser, |
| 104 | token_list_t *list, |
| 105 | token_list_t *result); |
| 106 | |
| 107 | static void |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 108 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition); |
| 109 | |
| 110 | static void |
| 111 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type, |
| 112 | int condition); |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 113 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 114 | static void |
| 115 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser); |
| 116 | |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 117 | #define yylex glcpp_parser_lex |
| 118 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 119 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 120 | glcpp_parser_lex (glcpp_parser_t *parser); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 121 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 122 | %} |
| 123 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 124 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 125 | %lex-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 126 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 127 | %token HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH_UNDEF IDENTIFIER NEWLINE OTHER SPACE |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 128 | %token LEFT_SHIFT RIGHT_SHIFT LESS_OR_EQUAL GREATER_OR_EQUAL EQUAL NOT_EQUAL AND OR PASTE |
Carl Worth | e939786 | 2010-05-25 17:08:07 -0700 | [diff] [blame] | 129 | %type <ival> punctuator SPACE |
| 130 | %type <str> IDENTIFIER OTHER |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 131 | %type <string_list> identifier_list |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 132 | %type <token> preprocessing_token |
| 133 | %type <token_list> pp_tokens replacement_list text_line |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 134 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 135 | /* Stale stuff just to allow code to compile. */ |
| 136 | %token IDENTIFIER_FINALIZED FUNC_MACRO OBJ_MACRO |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 137 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 138 | %% |
| 139 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 140 | input: |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 141 | /* empty */ |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 142 | | input line { |
| 143 | printf ("\n"); |
| 144 | } |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 145 | ; |
| 146 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 147 | line: |
| 148 | control_line |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 149 | | text_line { |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 150 | _glcpp_parser_print_expanded_token_list (parser, $1); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 151 | talloc_free ($1); |
| 152 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 153 | | HASH non_directive |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 154 | ; |
| 155 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 156 | control_line: |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 157 | HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE { |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 158 | _define_object_macro (parser, $2, $3); |
| 159 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 160 | | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE { |
| 161 | _define_function_macro (parser, $2, NULL, $5); |
| 162 | } |
| 163 | | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE { |
| 164 | _define_function_macro (parser, $2, $4, $6); |
| 165 | } |
Carl Worth | e6fb782 | 2010-05-25 15:28:58 -0700 | [diff] [blame] | 166 | | HASH_UNDEF IDENTIFIER NEWLINE { |
| 167 | string_list_t *macro = hash_table_find (parser->defines, $2); |
| 168 | if (macro) { |
| 169 | /* XXX: Need hash table to support a real way |
| 170 | * to remove an element rather than prefixing |
| 171 | * a new node with data of NULL like this. */ |
| 172 | hash_table_insert (parser->defines, NULL, $2); |
| 173 | talloc_free (macro); |
| 174 | } |
| 175 | talloc_free ($2); |
| 176 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 177 | | HASH NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 178 | ; |
| 179 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 180 | identifier_list: |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 181 | IDENTIFIER { |
| 182 | $$ = _string_list_create (parser); |
| 183 | _string_list_append_item ($$, $1); |
| 184 | talloc_steal ($$, $1); |
| 185 | } |
| 186 | | identifier_list ',' IDENTIFIER { |
| 187 | $$ = $1; |
| 188 | _string_list_append_item ($$, $3); |
| 189 | talloc_steal ($$, $3); |
| 190 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 191 | ; |
| 192 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 193 | text_line: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 194 | NEWLINE { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 195 | | pp_tokens NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 196 | ; |
| 197 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 198 | non_directive: |
| 199 | pp_tokens NEWLINE |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 200 | ; |
| 201 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 202 | replacement_list: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 203 | /* empty */ { $$ = NULL; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 204 | | pp_tokens |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 205 | ; |
| 206 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 207 | pp_tokens: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 208 | preprocessing_token { |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 209 | parser->space_tokens = 1; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 210 | $$ = _token_list_create (parser); |
| 211 | _token_list_append ($$, $1); |
| 212 | talloc_unlink (parser, $1); |
| 213 | } |
| 214 | | pp_tokens preprocessing_token { |
| 215 | $$ = $1; |
| 216 | _token_list_append ($$, $2); |
| 217 | talloc_unlink (parser, $2); |
| 218 | } |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 219 | ; |
| 220 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 221 | preprocessing_token: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 222 | IDENTIFIER { |
| 223 | $$ = _token_create_str (parser, IDENTIFIER, $1); |
| 224 | } |
| 225 | | punctuator { |
| 226 | $$ = _token_create_ival (parser, $1, $1); |
| 227 | } |
| 228 | | OTHER { |
| 229 | $$ = _token_create_str (parser, OTHER, $1); |
| 230 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 231 | | SPACE { |
Carl Worth | e939786 | 2010-05-25 17:08:07 -0700 | [diff] [blame] | 232 | $$ = _token_create_ival (parser, SPACE, SPACE); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 233 | } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 234 | ; |
| 235 | |
| 236 | punctuator: |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 237 | '[' { $$ = '['; } |
| 238 | | ']' { $$ = ']'; } |
| 239 | | '(' { $$ = '('; } |
| 240 | | ')' { $$ = ')'; } |
| 241 | | '{' { $$ = '{'; } |
| 242 | | '}' { $$ = '}'; } |
| 243 | | '.' { $$ = '.'; } |
| 244 | | '&' { $$ = '&'; } |
| 245 | | '*' { $$ = '*'; } |
| 246 | | '+' { $$ = '+'; } |
| 247 | | '-' { $$ = '-'; } |
| 248 | | '~' { $$ = '~'; } |
| 249 | | '!' { $$ = '!'; } |
| 250 | | '/' { $$ = '/'; } |
| 251 | | '%' { $$ = '%'; } |
| 252 | | LEFT_SHIFT { $$ = LEFT_SHIFT; } |
| 253 | | RIGHT_SHIFT { $$ = RIGHT_SHIFT; } |
| 254 | | '<' { $$ = '<'; } |
| 255 | | '>' { $$ = '>'; } |
| 256 | | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; } |
| 257 | | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; } |
| 258 | | EQUAL { $$ = EQUAL; } |
| 259 | | NOT_EQUAL { $$ = NOT_EQUAL; } |
| 260 | | '^' { $$ = '^'; } |
| 261 | | '|' { $$ = '|'; } |
| 262 | | AND { $$ = AND; } |
| 263 | | OR { $$ = OR; } |
| 264 | | ';' { $$ = ';'; } |
| 265 | | ',' { $$ = ','; } |
| 266 | | PASTE { $$ = PASTE; } |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 267 | ; |
| 268 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 269 | %% |
| 270 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 271 | string_list_t * |
| 272 | _string_list_create (void *ctx) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 273 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 274 | string_list_t *list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 275 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 276 | list = xtalloc (ctx, string_list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 277 | list->head = NULL; |
| 278 | list->tail = NULL; |
| 279 | |
| 280 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 281 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 282 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 283 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 284 | _string_list_append_list (string_list_t *list, string_list_t *tail) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 285 | { |
| 286 | if (list->head == NULL) { |
| 287 | list->head = tail->head; |
| 288 | } else { |
| 289 | list->tail->next = tail->head; |
| 290 | } |
| 291 | |
| 292 | list->tail = tail->tail; |
| 293 | } |
| 294 | |
| 295 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 296 | _string_list_append_item (string_list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 297 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 298 | string_node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 299 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 300 | node = xtalloc (list, string_node_t); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 301 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 302 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 303 | node->next = NULL; |
| 304 | |
| 305 | if (list->head == NULL) { |
| 306 | list->head = node; |
| 307 | } else { |
| 308 | list->tail->next = node; |
| 309 | } |
| 310 | |
| 311 | list->tail = node; |
| 312 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 313 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 314 | void |
| 315 | _string_list_push (string_list_t *list, const char *str) |
| 316 | { |
| 317 | string_node_t *node; |
| 318 | |
| 319 | node = xtalloc (list, string_node_t); |
| 320 | node->str = xtalloc_strdup (node, str); |
| 321 | node->next = list->head; |
| 322 | |
| 323 | if (list->tail == NULL) { |
| 324 | list->tail = node; |
| 325 | } |
| 326 | list->head = node; |
| 327 | } |
| 328 | |
| 329 | void |
| 330 | _string_list_pop (string_list_t *list) |
| 331 | { |
| 332 | string_node_t *node; |
| 333 | |
| 334 | node = list->head; |
| 335 | |
| 336 | if (node == NULL) { |
| 337 | fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n"); |
| 338 | exit (1); |
| 339 | } |
| 340 | |
| 341 | list->head = node->next; |
| 342 | if (list->tail == node) { |
| 343 | assert (node->next == NULL); |
| 344 | list->tail = NULL; |
| 345 | } |
| 346 | |
| 347 | talloc_free (node); |
| 348 | } |
| 349 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 350 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 351 | _string_list_contains (string_list_t *list, const char *member, int *index) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 352 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 353 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 354 | int i; |
| 355 | |
| 356 | if (list == NULL) |
| 357 | return 0; |
| 358 | |
| 359 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 360 | if (strcmp (node->str, member) == 0) { |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 361 | if (index) |
| 362 | *index = i; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 363 | return 1; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 371 | _string_list_length (string_list_t *list) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 372 | { |
| 373 | int length = 0; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 374 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 375 | |
| 376 | if (list == NULL) |
| 377 | return 0; |
| 378 | |
| 379 | for (node = list->head; node; node = node->next) |
| 380 | length++; |
| 381 | |
| 382 | return length; |
| 383 | } |
| 384 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 385 | argument_list_t * |
| 386 | _argument_list_create (void *ctx) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 387 | { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 388 | argument_list_t *list; |
| 389 | |
| 390 | list = xtalloc (ctx, argument_list_t); |
| 391 | list->head = NULL; |
| 392 | list->tail = NULL; |
| 393 | |
| 394 | return list; |
| 395 | } |
| 396 | |
| 397 | void |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 398 | _argument_list_append (argument_list_t *list, token_list_t *argument) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 399 | { |
| 400 | argument_node_t *node; |
| 401 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 402 | node = xtalloc (list, argument_node_t); |
| 403 | node->argument = argument; |
| 404 | |
| 405 | node->next = NULL; |
| 406 | |
| 407 | if (list->head == NULL) { |
| 408 | list->head = node; |
| 409 | } else { |
| 410 | list->tail->next = node; |
| 411 | } |
| 412 | |
| 413 | list->tail = node; |
| 414 | } |
| 415 | |
| 416 | int |
| 417 | _argument_list_length (argument_list_t *list) |
| 418 | { |
| 419 | int length = 0; |
| 420 | argument_node_t *node; |
| 421 | |
| 422 | if (list == NULL) |
| 423 | return 0; |
| 424 | |
| 425 | for (node = list->head; node; node = node->next) |
| 426 | length++; |
| 427 | |
| 428 | return length; |
| 429 | } |
| 430 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 431 | token_list_t * |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 432 | _argument_list_member_at (argument_list_t *list, int index) |
| 433 | { |
| 434 | argument_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 435 | int i; |
| 436 | |
| 437 | if (list == NULL) |
| 438 | return NULL; |
| 439 | |
| 440 | node = list->head; |
| 441 | for (i = 0; i < index; i++) { |
| 442 | node = node->next; |
| 443 | if (node == NULL) |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | if (node) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 448 | return node->argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 449 | |
| 450 | return NULL; |
| 451 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 452 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 453 | /* Note: This function talloc_steal()s the str pointer. */ |
| 454 | token_t * |
| 455 | _token_create_str (void *ctx, int type, char *str) |
| 456 | { |
| 457 | token_t *token; |
| 458 | |
| 459 | token = xtalloc (ctx, token_t); |
| 460 | token->type = type; |
| 461 | token->value.str = talloc_steal (token, str); |
| 462 | |
| 463 | return token; |
| 464 | } |
| 465 | |
| 466 | token_t * |
| 467 | _token_create_ival (void *ctx, int type, int ival) |
| 468 | { |
| 469 | token_t *token; |
| 470 | |
| 471 | token = xtalloc (ctx, token_t); |
| 472 | token->type = type; |
| 473 | token->value.ival = ival; |
| 474 | |
| 475 | return token; |
| 476 | } |
| 477 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 478 | token_list_t * |
| 479 | _token_list_create (void *ctx) |
| 480 | { |
| 481 | token_list_t *list; |
| 482 | |
| 483 | list = xtalloc (ctx, token_list_t); |
| 484 | list->head = NULL; |
| 485 | list->tail = NULL; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 486 | list->non_space_tail = NULL; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 487 | |
| 488 | return list; |
| 489 | } |
| 490 | |
| 491 | void |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 492 | _token_list_append (token_list_t *list, token_t *token) |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 493 | { |
| 494 | token_node_t *node; |
| 495 | |
| 496 | node = xtalloc (list, token_node_t); |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame] | 497 | node->token = xtalloc_reference (list, token); |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 498 | |
| 499 | node->next = NULL; |
| 500 | |
| 501 | if (list->head == NULL) { |
| 502 | list->head = node; |
| 503 | } else { |
| 504 | list->tail->next = node; |
| 505 | } |
| 506 | |
| 507 | list->tail = node; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 508 | if (token->type != SPACE) |
| 509 | list->non_space_tail = node; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void |
| 513 | _token_list_append_list (token_list_t *list, token_list_t *tail) |
| 514 | { |
| 515 | if (list->head == NULL) { |
| 516 | list->head = tail->head; |
| 517 | } else { |
| 518 | list->tail->next = tail->head; |
| 519 | } |
| 520 | |
| 521 | list->tail = tail->tail; |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 522 | list->non_space_tail = tail->non_space_tail; |
| 523 | } |
| 524 | |
| 525 | void |
| 526 | _token_list_trim_trailing_space (token_list_t *list) |
| 527 | { |
| 528 | token_node_t *tail, *next; |
| 529 | |
| 530 | if (list->non_space_tail) { |
| 531 | tail = list->non_space_tail->next; |
| 532 | list->non_space_tail->next = NULL; |
| 533 | list->tail = list->non_space_tail; |
| 534 | |
| 535 | while (tail) { |
| 536 | next = tail->next; |
| 537 | talloc_free (tail); |
| 538 | tail = next; |
| 539 | } |
| 540 | } |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 541 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 542 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 543 | static void |
| 544 | _token_print (token_t *token) |
| 545 | { |
| 546 | if (token->type < 256) { |
| 547 | printf ("%c", token->type); |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | switch (token->type) { |
| 552 | case IDENTIFIER: |
| 553 | case OTHER: |
| 554 | printf ("%s", token->value.str); |
| 555 | break; |
| 556 | case SPACE: |
| 557 | printf (" "); |
| 558 | break; |
| 559 | case LEFT_SHIFT: |
| 560 | printf ("<<"); |
| 561 | break; |
| 562 | case RIGHT_SHIFT: |
| 563 | printf (">>"); |
| 564 | break; |
| 565 | case LESS_OR_EQUAL: |
| 566 | printf ("<="); |
| 567 | break; |
| 568 | case GREATER_OR_EQUAL: |
| 569 | printf (">="); |
| 570 | break; |
| 571 | case EQUAL: |
| 572 | printf ("=="); |
| 573 | break; |
| 574 | case NOT_EQUAL: |
| 575 | printf ("!="); |
| 576 | break; |
| 577 | case AND: |
| 578 | printf ("&&"); |
| 579 | break; |
| 580 | case OR: |
| 581 | printf ("||"); |
| 582 | break; |
| 583 | case PASTE: |
| 584 | printf ("##"); |
| 585 | break; |
| 586 | default: |
| 587 | fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type); |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | static void |
| 593 | _token_list_print (token_list_t *list) |
| 594 | { |
| 595 | token_node_t *node; |
| 596 | |
| 597 | if (list == NULL) |
| 598 | return; |
| 599 | |
| 600 | for (node = list->head; node; node = node->next) |
| 601 | _token_print (node->token); |
| 602 | } |
| 603 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 604 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 605 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 606 | { |
| 607 | fprintf (stderr, "Parse error: %s\n", error); |
| 608 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 609 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 610 | glcpp_parser_t * |
| 611 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 612 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 613 | glcpp_parser_t *parser; |
| 614 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 615 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 616 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 617 | glcpp_lex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 618 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 619 | hash_table_string_compare); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 620 | parser->active = _string_list_create (parser); |
Carl Worth | f34a000 | 2010-05-25 16:59:02 -0700 | [diff] [blame] | 621 | parser->space_tokens = 1; |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame] | 622 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 623 | parser->skip_stack = NULL; |
| 624 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 625 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | int |
| 629 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 630 | { |
| 631 | return yyparse (parser); |
| 632 | } |
| 633 | |
| 634 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 635 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 636 | { |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 637 | if (parser->skip_stack) |
| 638 | fprintf (stderr, "Error: Unterminated #if\n"); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 639 | glcpp_lex_destroy (parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 640 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 641 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 642 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 643 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 644 | /* Appends onto 'expansion' a non-macro token or the expansion of an |
| 645 | * object-like macro. |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 646 | * |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 647 | * Returns 0 if this token is completely processed. |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 648 | * |
| 649 | * Returns 1 in the case that 'token' is a function-like macro that |
| 650 | * needs further expansion. |
| 651 | */ |
| 652 | static int |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 653 | _glcpp_parser_expand_token_onto (glcpp_parser_t *parser, |
| 654 | token_t *token, |
| 655 | token_list_t *result) |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 656 | { |
| 657 | const char *identifier; |
| 658 | macro_t *macro; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 659 | token_list_t *expansion; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 660 | |
| 661 | /* We only expand identifiers */ |
| 662 | if (token->type != IDENTIFIER) { |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 663 | _token_list_append (result, token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 664 | return 0; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /* Look up this identifier in the hash table. */ |
| 668 | identifier = token->value.str; |
| 669 | macro = hash_table_find (parser->defines, identifier); |
| 670 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 671 | /* Not a macro, so just append. */ |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 672 | if (macro == NULL) { |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 673 | _token_list_append (result, token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 674 | return 0; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 677 | /* Finally, don't expand this macro if we're already actively |
| 678 | * expanding it, (to avoid infinite recursion). */ |
Carl Worth | ec4ada0 | 2010-05-26 08:15:49 -0700 | [diff] [blame^] | 679 | if (_string_list_contains (parser->active, identifier, NULL)) |
| 680 | { |
| 681 | /* We change the token type here from IDENTIFIER to |
| 682 | * OTHER to prevent any future expansion of this |
| 683 | * unexpanded token. */ |
| 684 | char *str; |
| 685 | token_t *new_token; |
| 686 | |
| 687 | str = xtalloc_strdup (result, token->value.str); |
| 688 | new_token = _token_create_str (result, OTHER, str); |
| 689 | _token_list_append (result, new_token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 690 | return 0; |
| 691 | } |
| 692 | |
Carl Worth | c0607d5 | 2010-05-26 08:01:42 -0700 | [diff] [blame] | 693 | /* For function-like macros return 1 for further processing. */ |
| 694 | if (macro->is_function) { |
| 695 | return 1; |
| 696 | } |
| 697 | |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 698 | _string_list_push (parser->active, identifier); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 699 | _glcpp_parser_expand_token_list_onto (parser, |
| 700 | macro->replacements, |
| 701 | result); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 702 | _string_list_pop (parser->active); |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | typedef enum function_status |
| 708 | { |
| 709 | FUNCTION_STATUS_SUCCESS, |
| 710 | FUNCTION_NOT_A_FUNCTION, |
| 711 | FUNCTION_UNBALANCED_PARENTHESES |
| 712 | } function_status_t; |
| 713 | |
| 714 | /* Find a set of function-like macro arguments by looking for a |
| 715 | * balanced set of parentheses. Upon return *node will be the last |
| 716 | * consumed node, such that further processing can continue with |
| 717 | * node->next. |
| 718 | * |
| 719 | * Return values: |
| 720 | * |
| 721 | * FUNCTION_STATUS_SUCCESS: |
| 722 | * |
| 723 | * Successfully parsed a set of function arguments. |
| 724 | * |
| 725 | * FUNCTION_NOT_A_FUNCTION: |
| 726 | * |
| 727 | * Macro name not followed by a '('. This is not an error, but |
| 728 | * simply that the macro name should be treated as a non-macro. |
| 729 | * |
| 730 | * FUNCTION_UNBLANCED_PARENTHESES |
| 731 | * |
| 732 | * Macro name is not followed by a balanced set of parentheses. |
| 733 | */ |
| 734 | static function_status_t |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 735 | _arguments_parse (argument_list_t *arguments, token_node_t **node_ret) |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 736 | { |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 737 | token_list_t *argument; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 738 | token_node_t *node = *node_ret, *last; |
| 739 | int paren_count; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 740 | |
| 741 | last = node; |
| 742 | node = node->next; |
| 743 | |
| 744 | /* Ignore whitespace before first parenthesis. */ |
| 745 | while (node && node->token->type == SPACE) |
| 746 | node = node->next; |
| 747 | |
| 748 | if (node == NULL || node->token->type != '(') |
| 749 | return FUNCTION_NOT_A_FUNCTION; |
| 750 | |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 751 | last = node; |
| 752 | node = node->next; |
| 753 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 754 | argument = NULL; |
| 755 | |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 756 | for (paren_count = 1; node; last = node, node = node->next) { |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 757 | if (node->token->type == '(') |
| 758 | { |
| 759 | paren_count++; |
| 760 | } |
| 761 | else if (node->token->type == ')') |
| 762 | { |
| 763 | paren_count--; |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 764 | if (paren_count == 0) { |
| 765 | last = node; |
| 766 | node = node->next; |
| 767 | break; |
| 768 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 769 | } |
Carl Worth | 652fa27 | 2010-05-25 17:45:22 -0700 | [diff] [blame] | 770 | |
| 771 | if (node->token->type == ',' && |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 772 | paren_count == 1) |
| 773 | { |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 774 | if (argument) |
| 775 | _token_list_trim_trailing_space (argument); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 776 | argument = NULL; |
| 777 | } |
| 778 | else { |
| 779 | if (argument == NULL) { |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 780 | /* Don't treat initial whitespace as |
| 781 | * part of the arguement. */ |
| 782 | if (node->token->type == SPACE) |
| 783 | continue; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 784 | argument = _token_list_create (arguments); |
| 785 | _argument_list_append (arguments, argument); |
| 786 | } |
| 787 | _token_list_append (argument, node->token); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 788 | } |
Carl Worth | c7581c2 | 2010-05-25 17:41:07 -0700 | [diff] [blame] | 789 | } |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 790 | |
| 791 | if (node && paren_count) |
| 792 | return FUNCTION_UNBALANCED_PARENTHESES; |
| 793 | |
| 794 | *node_ret = last; |
| 795 | |
| 796 | return FUNCTION_STATUS_SUCCESS; |
| 797 | } |
| 798 | |
| 799 | /* Prints the expansion of *node (consuming further tokens from the |
| 800 | * list as necessary). Upon return *node will be the last consumed |
| 801 | * node, such that further processing can continue with node->next. */ |
| 802 | static void |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 803 | _glcpp_parser_expand_function_onto (glcpp_parser_t *parser, |
| 804 | token_node_t **node_ret, |
| 805 | token_list_t *result) |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 806 | { |
| 807 | macro_t *macro; |
| 808 | token_node_t *node; |
| 809 | const char *identifier; |
| 810 | argument_list_t *arguments; |
| 811 | function_status_t status; |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 812 | token_list_t *substituted; |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 813 | token_node_t *i, *j; |
| 814 | int parameter_index; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 815 | |
| 816 | node = *node_ret; |
| 817 | identifier = node->token->value.str; |
| 818 | |
| 819 | macro = hash_table_find (parser->defines, identifier); |
| 820 | |
| 821 | assert (macro->is_function); |
| 822 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 823 | arguments = _argument_list_create (parser); |
| 824 | status = _arguments_parse (arguments, node_ret); |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 825 | |
| 826 | switch (status) { |
| 827 | case FUNCTION_STATUS_SUCCESS: |
| 828 | break; |
| 829 | case FUNCTION_NOT_A_FUNCTION: |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 830 | _token_list_append (result, node->token); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 831 | return; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 832 | case FUNCTION_UNBALANCED_PARENTHESES: |
| 833 | fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n", |
| 834 | identifier); |
| 835 | exit (1); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 838 | if (macro->replacements == NULL) { |
| 839 | talloc_free (arguments); |
| 840 | return; |
| 841 | } |
| 842 | |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 843 | if (_argument_list_length (arguments) != |
| 844 | _string_list_length (macro->parameters)) |
| 845 | { |
| 846 | fprintf (stderr, |
| 847 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 848 | identifier, |
| 849 | _argument_list_length (arguments), |
| 850 | _string_list_length (macro->parameters)); |
| 851 | return; |
| 852 | } |
| 853 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 854 | /* Perform argument substitution on the replacement list. */ |
| 855 | substituted = _token_list_create (arguments); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 856 | |
| 857 | for (i = macro->replacements->head; i; i = i->next) { |
| 858 | if (i->token->type == IDENTIFIER && |
| 859 | _string_list_contains (macro->parameters, |
| 860 | i->token->value.str, |
| 861 | ¶meter_index)) |
| 862 | { |
| 863 | token_list_t *argument; |
| 864 | argument = _argument_list_member_at (arguments, |
| 865 | parameter_index); |
Carl Worth | d5cd403 | 2010-05-26 08:09:29 -0700 | [diff] [blame] | 866 | /* Before substituting, we expand the argument |
| 867 | * tokens. */ |
| 868 | _glcpp_parser_expand_token_list_onto (parser, |
| 869 | argument, |
| 870 | substituted); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 871 | } else { |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 872 | _token_list_append (substituted, i->token); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 876 | _string_list_push (parser->active, identifier); |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 877 | _glcpp_parser_expand_token_list_onto (parser, substituted, result); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 878 | _string_list_pop (parser->active); |
Carl Worth | 9ce18cf | 2010-05-25 17:32:21 -0700 | [diff] [blame] | 879 | |
| 880 | talloc_free (arguments); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 883 | static void |
| 884 | _glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser, |
| 885 | token_list_t *list, |
| 886 | token_list_t *result) |
| 887 | { |
| 888 | token_node_t *node; |
| 889 | |
| 890 | if (list == NULL) |
| 891 | return; |
| 892 | |
| 893 | for (node = list->head; node; node = node->next) |
| 894 | { |
| 895 | if (_glcpp_parser_expand_token_onto (parser, node->token, |
| 896 | result)) |
| 897 | { |
| 898 | _glcpp_parser_expand_function_onto (parser, &node, |
| 899 | result); |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 904 | void |
| 905 | _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser, |
| 906 | token_list_t *list) |
| 907 | { |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 908 | token_list_t *expanded; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 909 | token_node_t *node; |
Carl Worth | b1854fd | 2010-05-25 16:28:26 -0700 | [diff] [blame] | 910 | function_status_t function_status; |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 911 | |
| 912 | if (list == NULL) |
| 913 | return; |
| 914 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 915 | expanded = _token_list_create (parser); |
Carl Worth | 10ae438 | 2010-05-25 20:35:01 -0700 | [diff] [blame] | 916 | |
Carl Worth | 0197e9b | 2010-05-26 08:05:19 -0700 | [diff] [blame] | 917 | _glcpp_parser_expand_token_list_onto (parser, list, expanded); |
| 918 | |
| 919 | _token_list_trim_trailing_space (expanded); |
| 920 | |
| 921 | _token_list_print (expanded); |
| 922 | |
| 923 | talloc_free (expanded); |
Carl Worth | ae6517f | 2010-05-25 15:24:59 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 927 | _define_object_macro (glcpp_parser_t *parser, |
| 928 | const char *identifier, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 929 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 930 | { |
| 931 | macro_t *macro; |
| 932 | |
| 933 | macro = xtalloc (parser, macro_t); |
| 934 | |
| 935 | macro->is_function = 0; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 936 | macro->parameters = NULL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 937 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 938 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 939 | |
| 940 | hash_table_insert (parser->defines, macro, identifier); |
| 941 | } |
| 942 | |
| 943 | void |
| 944 | _define_function_macro (glcpp_parser_t *parser, |
| 945 | const char *identifier, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 946 | string_list_t *parameters, |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 947 | token_list_t *replacements) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 948 | { |
| 949 | macro_t *macro; |
| 950 | |
| 951 | macro = xtalloc (parser, macro_t); |
| 952 | |
| 953 | macro->is_function = 1; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 954 | macro->parameters = talloc_steal (macro, parameters); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 955 | macro->identifier = talloc_strdup (macro, identifier); |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 956 | macro->replacements = talloc_steal (macro, replacements); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 957 | |
| 958 | hash_table_insert (parser->defines, macro, identifier); |
| 959 | } |
| 960 | |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 961 | static int |
Carl Worth | 0293b2e | 2010-05-19 10:05:40 -0700 | [diff] [blame] | 962 | glcpp_parser_lex (glcpp_parser_t *parser) |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 963 | { |
Carl Worth | 5aa7ea0 | 2010-05-25 18:39:43 -0700 | [diff] [blame] | 964 | return glcpp_lex (parser->scanner); |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 965 | } |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 966 | |
| 967 | static void |
| 968 | _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition) |
| 969 | { |
| 970 | skip_type_t current = SKIP_NO_SKIP; |
| 971 | skip_node_t *node; |
| 972 | |
| 973 | if (parser->skip_stack) |
| 974 | current = parser->skip_stack->type; |
| 975 | |
| 976 | node = xtalloc (parser, skip_node_t); |
| 977 | |
| 978 | if (current == SKIP_NO_SKIP) { |
| 979 | if (condition) |
| 980 | node->type = SKIP_NO_SKIP; |
| 981 | else |
| 982 | node->type = SKIP_TO_ELSE; |
| 983 | } else { |
| 984 | node->type = SKIP_TO_ENDIF; |
| 985 | } |
| 986 | |
| 987 | node->next = parser->skip_stack; |
| 988 | parser->skip_stack = node; |
| 989 | } |
| 990 | |
| 991 | static void |
| 992 | _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type, |
| 993 | int condition) |
| 994 | { |
| 995 | if (parser->skip_stack == NULL) { |
| 996 | fprintf (stderr, "Error: %s without #if\n", type); |
| 997 | exit (1); |
| 998 | } |
| 999 | |
| 1000 | if (parser->skip_stack->type == SKIP_TO_ELSE) { |
| 1001 | if (condition) |
| 1002 | parser->skip_stack->type = SKIP_NO_SKIP; |
| 1003 | } else { |
| 1004 | parser->skip_stack->type = SKIP_TO_ENDIF; |
| 1005 | } |
| 1006 | } |
Carl Worth | 80dc60b | 2010-05-25 14:42:00 -0700 | [diff] [blame] | 1007 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 1008 | static void |
| 1009 | _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser) |
| 1010 | { |
| 1011 | skip_node_t *node; |
| 1012 | |
| 1013 | if (parser->skip_stack == NULL) { |
| 1014 | fprintf (stderr, "Error: #endif without #if\n"); |
| 1015 | exit (1); |
| 1016 | } |
| 1017 | |
| 1018 | node = parser->skip_stack; |
| 1019 | parser->skip_stack = node->next; |
| 1020 | talloc_free (node); |
| 1021 | } |