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 | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 31 | #define YYLEX_PARAM parser->scanner |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 32 | |
| 33 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 34 | yyerror (void *scanner, const char *error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 35 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 36 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 37 | _define_object_macro (glcpp_parser_t *parser, |
| 38 | const char *macro, |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 39 | const char *replacement); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 40 | |
| 41 | void |
| 42 | _define_function_macro (glcpp_parser_t *parser, |
| 43 | const char *macro, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 44 | string_list_t *parameters, |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 45 | const char *replacement); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 46 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 47 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 48 | _expand_object_macro (glcpp_parser_t *parser, const char *identifier); |
| 49 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 50 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 51 | _expand_function_macro (glcpp_parser_t *parser, |
| 52 | const char *identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 53 | argument_list_t *arguments); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 54 | |
| 55 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 56 | _print_string_list (string_list_t *list); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 57 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 58 | string_list_t * |
| 59 | _string_list_create (void *ctx); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 60 | |
| 61 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 62 | _string_list_append_item (string_list_t *list, const char *str); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 63 | |
| 64 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 65 | _string_list_append_list (string_list_t *list, string_list_t *tail); |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 66 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 67 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 68 | _string_list_contains (string_list_t *list, const char *member, int *index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 69 | |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 70 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 71 | _string_list_length (string_list_t *list); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 72 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 73 | argument_list_t * |
| 74 | _argument_list_create (void *ctx); |
| 75 | |
| 76 | void |
| 77 | _argument_list_append (argument_list_t *list, string_list_t *argument); |
| 78 | |
| 79 | int |
| 80 | _argument_list_length (argument_list_t *list); |
| 81 | |
| 82 | string_list_t * |
| 83 | _argument_list_member_at (argument_list_t *list, int index); |
| 84 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 85 | %} |
| 86 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 87 | %union { |
| 88 | char *str; |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 89 | string_list_t *string_list; |
| 90 | argument_list_t *argument_list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 93 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 94 | %lex-param {void *scanner} |
| 95 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 96 | %token DEFINE FUNC_MACRO IDENTIFIER NEWLINE OBJ_MACRO REPLACEMENT TOKEN UNDEF |
| 97 | %type <str> FUNC_MACRO IDENTIFIER OBJ_MACRO REPLACEMENT TOKEN word |
| 98 | %type <string_list> argument macro parameter_list |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 99 | %type <argument_list> argument_list |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 100 | |
Carl Worth | 796e1f0 | 2010-05-17 12:45:16 -0700 | [diff] [blame] | 101 | /* Hard to remove shift/reduce conflicts documented as follows: |
| 102 | * |
| 103 | * 1. '(' after FUNC_MACRO name which is correctly resolved to shift |
| 104 | * to form macro invocation rather than reducing directly to |
| 105 | * content. |
| 106 | */ |
| 107 | %expect 1 |
| 108 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 109 | %% |
| 110 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 111 | input: |
| 112 | /* empty */ |
Carl Worth | 8bcb6f1 | 2010-05-12 13:21:20 -0700 | [diff] [blame] | 113 | | input content |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 114 | ; |
| 115 | |
Carl Worth | 04af135 | 2010-05-14 10:17:38 -0700 | [diff] [blame] | 116 | /* We do all printing at the content level */ |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 117 | content: |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 118 | IDENTIFIER { |
| 119 | printf ("%s", $1); |
| 120 | talloc_free ($1); |
| 121 | } |
| 122 | | TOKEN { |
| 123 | printf ("%s", $1); |
| 124 | talloc_free ($1); |
| 125 | } |
Carl Worth | acf87bc | 2010-05-17 10:34:29 -0700 | [diff] [blame] | 126 | | FUNC_MACRO { |
| 127 | printf ("%s", $1); |
| 128 | talloc_free ($1); |
| 129 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 130 | | directive { |
| 131 | printf ("\n"); |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 132 | } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 133 | | '(' { printf ("("); } |
| 134 | | ')' { printf (")"); } |
| 135 | | ',' { printf (","); } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 136 | | macro |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 137 | ; |
| 138 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 139 | macro: |
| 140 | FUNC_MACRO '(' argument_list ')' { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 141 | _expand_function_macro (parser, $1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 142 | } |
| 143 | | OBJ_MACRO { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 144 | _expand_object_macro (parser, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 145 | talloc_free ($1); |
| 146 | } |
| 147 | ; |
| 148 | |
| 149 | argument_list: |
Carl Worth | ac070e8 | 2010-05-14 11:33:00 -0700 | [diff] [blame] | 150 | /* empty */ { |
| 151 | $$ = _argument_list_create (parser); |
| 152 | } |
| 153 | | argument { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 154 | $$ = _argument_list_create (parser); |
| 155 | _argument_list_append ($$, $1); |
| 156 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 157 | | argument_list ',' argument { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 158 | _argument_list_append ($1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 159 | $$ = $1; |
| 160 | } |
| 161 | ; |
| 162 | |
| 163 | argument: |
Carl Worth | ac070e8 | 2010-05-14 11:33:00 -0700 | [diff] [blame] | 164 | word { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 165 | $$ = _string_list_create (parser); |
Carl Worth | ac070e8 | 2010-05-14 11:33:00 -0700 | [diff] [blame] | 166 | _string_list_append_item ($$, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 167 | } |
Carl Worth | 38bd27b | 2010-05-14 12:05:37 -0700 | [diff] [blame] | 168 | | macro { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 169 | $$ = _string_list_create (parser); |
Carl Worth | 38bd27b | 2010-05-14 12:05:37 -0700 | [diff] [blame] | 170 | } |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 171 | | argument word { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 172 | _string_list_append_item ($1, $2); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 173 | talloc_free ($2); |
Carl Worth | 3596bb1 | 2010-05-14 16:53:52 -0700 | [diff] [blame] | 174 | $$ = $1; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 175 | } |
Carl Worth | 3596bb1 | 2010-05-14 16:53:52 -0700 | [diff] [blame] | 176 | | argument '(' argument ')' { |
| 177 | _string_list_append_item ($1, "("); |
| 178 | _string_list_append_list ($1, $3); |
| 179 | _string_list_append_item ($1, ")"); |
| 180 | $$ = $1; |
| 181 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 182 | ; |
| 183 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 184 | directive: |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 185 | DEFINE IDENTIFIER REPLACEMENT { |
| 186 | _define_object_macro (parser, $2, $3); |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 187 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 188 | | DEFINE IDENTIFIER '(' parameter_list ')' REPLACEMENT { |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 189 | _define_function_macro (parser, $2, $4, $6); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 190 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 191 | | UNDEF IDENTIFIER { |
| 192 | string_list_t *macro = hash_table_find (parser->defines, $2); |
| 193 | if (macro) { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 194 | /* XXX: Need hash table to support a real way |
| 195 | * to remove an element rather than prefixing |
| 196 | * a new node with data of NULL like this. */ |
| 197 | hash_table_insert (parser->defines, NULL, $2); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 198 | talloc_free (macro); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 199 | } |
| 200 | talloc_free ($2); |
| 201 | } |
Carl Worth | 38bd27b | 2010-05-14 12:05:37 -0700 | [diff] [blame] | 202 | ; |
| 203 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 204 | parameter_list: |
| 205 | /* empty */ { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 206 | $$ = _string_list_create (parser); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 207 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 208 | | IDENTIFIER { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 209 | $$ = _string_list_create (parser); |
| 210 | _string_list_append_item ($$, $1); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 211 | talloc_free ($1); |
| 212 | } |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 213 | | parameter_list ',' IDENTIFIER { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 214 | _string_list_append_item ($1, $3); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 215 | talloc_free ($3); |
| 216 | $$ = $1; |
| 217 | } |
| 218 | ; |
| 219 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 220 | word: |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 221 | IDENTIFIER { $$ = $1; } |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 222 | | TOKEN { $$ = $1; } |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 223 | ; |
| 224 | |
| 225 | %% |
| 226 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 227 | string_list_t * |
| 228 | _string_list_create (void *ctx) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 229 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 230 | string_list_t *list; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 231 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 232 | list = xtalloc (ctx, string_list_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 233 | list->head = NULL; |
| 234 | list->tail = NULL; |
| 235 | |
| 236 | return list; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 237 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 238 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 239 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 240 | _string_list_append_list (string_list_t *list, string_list_t *tail) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 241 | { |
| 242 | if (list->head == NULL) { |
| 243 | list->head = tail->head; |
| 244 | } else { |
| 245 | list->tail->next = tail->head; |
| 246 | } |
| 247 | |
| 248 | list->tail = tail->tail; |
| 249 | } |
| 250 | |
| 251 | void |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 252 | _string_list_append_item (string_list_t *list, const char *str) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 253 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 254 | string_node_t *node; |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 255 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 256 | node = xtalloc (list, string_node_t); |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 257 | node->str = xtalloc_strdup (node, str); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 258 | |
| 259 | node->next = NULL; |
| 260 | |
| 261 | if (list->head == NULL) { |
| 262 | list->head = node; |
| 263 | } else { |
| 264 | list->tail->next = node; |
| 265 | } |
| 266 | |
| 267 | list->tail = node; |
| 268 | } |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 269 | |
| 270 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 271 | _string_list_contains (string_list_t *list, const char *member, int *index) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 272 | { |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 273 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 274 | int i; |
| 275 | |
| 276 | if (list == NULL) |
| 277 | return 0; |
| 278 | |
| 279 | for (i = 0, node = list->head; node; i++, node = node->next) { |
| 280 | if (strcmp (node->str, member) == 0) { |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 281 | if (index) |
| 282 | *index = i; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 283 | return 1; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | int |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 291 | _string_list_length (string_list_t *list) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 292 | { |
| 293 | int length = 0; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 294 | string_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 295 | |
| 296 | if (list == NULL) |
| 297 | return 0; |
| 298 | |
| 299 | for (node = list->head; node; node = node->next) |
| 300 | length++; |
| 301 | |
| 302 | return length; |
| 303 | } |
| 304 | |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 305 | void |
| 306 | _print_string_list (string_list_t *list) |
| 307 | { |
| 308 | string_node_t *node; |
| 309 | |
| 310 | if (list == NULL) |
| 311 | return; |
| 312 | |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 313 | for (node = list->head; node; node = node->next) { |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 314 | printf ("%s", node->str); |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 315 | if (node->next) |
| 316 | printf (" "); |
| 317 | } |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 320 | argument_list_t * |
| 321 | _argument_list_create (void *ctx) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 322 | { |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 323 | argument_list_t *list; |
| 324 | |
| 325 | list = xtalloc (ctx, argument_list_t); |
| 326 | list->head = NULL; |
| 327 | list->tail = NULL; |
| 328 | |
| 329 | return list; |
| 330 | } |
| 331 | |
| 332 | void |
| 333 | _argument_list_append (argument_list_t *list, string_list_t *argument) |
| 334 | { |
| 335 | argument_node_t *node; |
| 336 | |
| 337 | if (argument == NULL || argument->head == NULL) |
| 338 | return; |
| 339 | |
| 340 | node = xtalloc (list, argument_node_t); |
| 341 | node->argument = argument; |
| 342 | |
| 343 | node->next = NULL; |
| 344 | |
| 345 | if (list->head == NULL) { |
| 346 | list->head = node; |
| 347 | } else { |
| 348 | list->tail->next = node; |
| 349 | } |
| 350 | |
| 351 | list->tail = node; |
| 352 | } |
| 353 | |
| 354 | int |
| 355 | _argument_list_length (argument_list_t *list) |
| 356 | { |
| 357 | int length = 0; |
| 358 | argument_node_t *node; |
| 359 | |
| 360 | if (list == NULL) |
| 361 | return 0; |
| 362 | |
| 363 | for (node = list->head; node; node = node->next) |
| 364 | length++; |
| 365 | |
| 366 | return length; |
| 367 | } |
| 368 | |
| 369 | string_list_t * |
| 370 | _argument_list_member_at (argument_list_t *list, int index) |
| 371 | { |
| 372 | argument_node_t *node; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 373 | int i; |
| 374 | |
| 375 | if (list == NULL) |
| 376 | return NULL; |
| 377 | |
| 378 | node = list->head; |
| 379 | for (i = 0; i < index; i++) { |
| 380 | node = node->next; |
| 381 | if (node == NULL) |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | if (node) |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 386 | return node->argument; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 387 | |
| 388 | return NULL; |
| 389 | } |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 390 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 391 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 392 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 393 | { |
| 394 | fprintf (stderr, "Parse error: %s\n", error); |
| 395 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 396 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 397 | glcpp_parser_t * |
| 398 | glcpp_parser_create (void) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 399 | { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 400 | glcpp_parser_t *parser; |
| 401 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 402 | parser = xtalloc (NULL, glcpp_parser_t); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 403 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 404 | yylex_init_extra (parser, &parser->scanner); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 405 | parser->defines = hash_table_ctor (32, hash_table_string_hash, |
| 406 | hash_table_string_compare); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 407 | parser->expansions = NULL; |
| 408 | |
| 409 | parser->lex_stack = xtalloc (parser, glcpp_lex_stack_t); |
| 410 | parser->lex_stack->parser = parser; |
| 411 | parser->lex_stack->head = NULL; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 412 | |
| 413 | return parser; |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | int |
| 417 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 418 | { |
| 419 | return yyparse (parser); |
| 420 | } |
| 421 | |
| 422 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 423 | glcpp_parser_destroy (glcpp_parser_t *parser) |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 424 | { |
| 425 | yylex_destroy (parser->scanner); |
| 426 | hash_table_dtor (parser->defines); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 427 | talloc_free (parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 428 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame] | 429 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame^] | 430 | static int |
| 431 | glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member) |
| 432 | { |
| 433 | expansion_node_t *node; |
| 434 | |
| 435 | for (node = parser->expansions; node; node = node->next) { |
| 436 | if (node->macro && |
| 437 | strcmp (node->macro->identifier, member) == 0) |
| 438 | { |
| 439 | return 1; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 446 | token_class_t |
| 447 | glcpp_parser_classify_token (glcpp_parser_t *parser, |
| 448 | const char *identifier, |
| 449 | int *parameter_index) |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 450 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 451 | macro_t *macro; |
| 452 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 453 | /* First we check if we are currently expanding a |
| 454 | * function-like macro, and if so, whether the parameter list |
| 455 | * contains a parameter matching this token name. */ |
| 456 | if (parser->expansions && |
| 457 | parser->expansions->macro && |
| 458 | parser->expansions->macro->parameters) |
| 459 | { |
| 460 | string_list_t *list; |
| 461 | |
| 462 | list = parser->expansions->macro->parameters; |
| 463 | |
| 464 | if (_string_list_contains (list, identifier, parameter_index)) |
| 465 | return TOKEN_CLASS_ARGUMENT; |
| 466 | } |
| 467 | |
| 468 | /* If not a function-like macro parameter, we next check if |
| 469 | * this token is a macro itself. */ |
| 470 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 471 | macro = hash_table_find (parser->defines, identifier); |
| 472 | |
| 473 | if (macro == NULL) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 474 | return TOKEN_CLASS_IDENTIFIER; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 475 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame^] | 476 | /* Don't consider this a macro if we are already actively |
| 477 | * expanding this macro. */ |
| 478 | if (glcpp_parser_is_expanding (parser, identifier)) |
| 479 | return TOKEN_CLASS_IDENTIFIER; |
| 480 | |
| 481 | /* Definitely a macro. Just need to check if it's function-like. */ |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 482 | if (macro->is_function) |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 483 | return TOKEN_CLASS_FUNC_MACRO; |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 484 | else |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 485 | return TOKEN_CLASS_OBJ_MACRO; |
Carl Worth | 9f62a7e | 2010-05-13 07:38:29 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 488 | void |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 489 | _define_object_macro (glcpp_parser_t *parser, |
| 490 | const char *identifier, |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 491 | const char *replacement) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 492 | { |
| 493 | macro_t *macro; |
| 494 | |
| 495 | macro = xtalloc (parser, macro_t); |
| 496 | |
| 497 | macro->is_function = 0; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 498 | macro->parameters = NULL; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 499 | macro->identifier = talloc_strdup (macro, identifier); |
| 500 | macro->replacement = talloc_steal (macro, replacement); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 501 | |
| 502 | hash_table_insert (parser->defines, macro, identifier); |
| 503 | } |
| 504 | |
| 505 | void |
| 506 | _define_function_macro (glcpp_parser_t *parser, |
| 507 | const char *identifier, |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 508 | string_list_t *parameters, |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 509 | const char *replacement) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 510 | { |
| 511 | macro_t *macro; |
| 512 | |
| 513 | macro = xtalloc (parser, macro_t); |
| 514 | |
| 515 | macro->is_function = 1; |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 516 | macro->parameters = talloc_steal (macro, parameters); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 517 | macro->identifier = talloc_strdup (macro, identifier); |
| 518 | macro->replacement = talloc_steal (macro, replacement); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 519 | |
| 520 | hash_table_insert (parser->defines, macro, identifier); |
| 521 | } |
| 522 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 523 | static void |
| 524 | _glcpp_parser_push_expansion_internal (glcpp_parser_t *parser, |
| 525 | macro_t *macro, |
| 526 | argument_list_t *arguments, |
| 527 | const char * replacement) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 528 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 529 | expansion_node_t *node; |
| 530 | |
| 531 | node = xtalloc (parser, expansion_node_t); |
| 532 | |
| 533 | node->macro = macro; |
| 534 | node->arguments = arguments; |
| 535 | |
| 536 | node->next = parser->expansions; |
| 537 | parser->expansions = node; |
| 538 | |
| 539 | glcpp_lex_stack_push (parser->lex_stack, replacement); |
| 540 | } |
| 541 | |
| 542 | void |
| 543 | glcpp_parser_push_expansion_macro (glcpp_parser_t *parser, |
| 544 | macro_t *macro, |
| 545 | argument_list_t *arguments) |
| 546 | { |
| 547 | _glcpp_parser_push_expansion_internal (parser, macro, arguments, |
| 548 | macro->replacement); |
| 549 | } |
| 550 | |
| 551 | void |
| 552 | glcpp_parser_push_expansion_argument (glcpp_parser_t *parser, |
| 553 | int argument_index) |
| 554 | { |
| 555 | argument_list_t *arguments; |
| 556 | string_list_t *argument; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 557 | string_node_t *node; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 558 | char *argument_str, *s; |
| 559 | int length; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 560 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 561 | arguments = parser->expansions->arguments; |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 562 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 563 | argument = _argument_list_member_at (arguments, argument_index); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 564 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 565 | length = 0; |
| 566 | for (node = argument->head; node; node = node->next) |
| 567 | length += strlen (node->str) + 1; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 568 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 569 | argument_str = xtalloc_size (parser, length); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 570 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 571 | *argument_str = '\0'; |
| 572 | s = argument_str; |
| 573 | for (node = argument->head; node; node = node->next) { |
| 574 | strcpy (s, node->str); |
| 575 | s += strlen (node->str); |
| 576 | if (node->next) { |
| 577 | *s = ' '; |
| 578 | s++; |
| 579 | *s = '\0'; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 580 | } |
| 581 | } |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 582 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 583 | _glcpp_parser_push_expansion_internal (parser, NULL, NULL, |
| 584 | argument_str); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 587 | /* The lexer calls this when it exhausts a string. */ |
| 588 | void |
| 589 | glcpp_parser_pop_expansion (glcpp_parser_t *parser) |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 590 | { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 591 | expansion_node_t *node; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 592 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 593 | node = parser->expansions; |
Carl Worth | 420d05a | 2010-05-17 10:15:23 -0700 | [diff] [blame] | 594 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 595 | if (node == NULL) { |
| 596 | fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n"); |
| 597 | exit (1); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 600 | parser->expansions = node->next; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 601 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 602 | talloc_free (node); |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 605 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 606 | _expand_object_macro (glcpp_parser_t *parser, const char *identifier) |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 607 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 608 | macro_t *macro; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 609 | |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 610 | macro = hash_table_find (parser->defines, identifier); |
| 611 | assert (! macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame^] | 612 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 613 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame^] | 614 | glcpp_parser_push_expansion_macro (parser, macro, NULL); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 617 | void |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 618 | _expand_function_macro (glcpp_parser_t *parser, |
| 619 | const char *identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 620 | argument_list_t *arguments) |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 621 | { |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 622 | macro_t *macro; |
| 623 | |
| 624 | macro = hash_table_find (parser->defines, identifier); |
| 625 | assert (macro->is_function); |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame^] | 626 | assert (! glcpp_parser_is_expanding (parser, identifier)); |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 627 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 628 | if (_argument_list_length (arguments) != |
Carl Worth | 2be8be0 | 2010-05-14 10:31:43 -0700 | [diff] [blame] | 629 | _string_list_length (macro->parameters)) |
| 630 | { |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 631 | fprintf (stderr, |
| 632 | "Error: macro %s invoked with %d arguments (expected %d)\n", |
| 633 | identifier, |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 634 | _argument_list_length (arguments), |
Carl Worth | c5e9855 | 2010-05-14 10:12:21 -0700 | [diff] [blame] | 635 | _string_list_length (macro->parameters)); |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 636 | return; |
Carl Worth | dcc2ecd | 2010-05-13 12:56:42 -0700 | [diff] [blame] | 637 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 638 | |
Carl Worth | be0e2e9 | 2010-05-19 07:29:22 -0700 | [diff] [blame^] | 639 | glcpp_parser_push_expansion_macro (parser, macro, arguments); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 640 | } |