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