2010-05-10 11:44:09 -07:00
|
|
|
%{
|
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2010 Intel Corporation
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2010-05-13 09:36:23 -07:00
|
|
|
#include <assert.h>
|
2010-05-12 12:17:10 -07:00
|
|
|
#include <talloc.h>
|
2010-05-10 11:44:09 -07:00
|
|
|
|
2010-05-10 13:17:25 -07:00
|
|
|
#include "glcpp.h"
|
|
|
|
|
|
2010-05-10 16:16:06 -07:00
|
|
|
#define YYLEX_PARAM parser->scanner
|
2010-05-10 11:44:09 -07:00
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
typedef struct {
|
|
|
|
|
int is_function;
|
2010-05-14 10:12:21 -07:00
|
|
|
string_list_t *parameters;
|
|
|
|
|
string_list_t *replacements;
|
2010-05-13 09:36:23 -07:00
|
|
|
} macro_t;
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
struct glcpp_parser {
|
|
|
|
|
yyscan_t scanner;
|
|
|
|
|
struct hash_table *defines;
|
|
|
|
|
};
|
|
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
void
|
2010-05-10 13:17:25 -07:00
|
|
|
yyerror (void *scanner, const char *error);
|
2010-05-10 11:44:09 -07:00
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
void
|
2010-05-13 09:36:23 -07:00
|
|
|
_define_object_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *macro,
|
2010-05-14 10:12:21 -07:00
|
|
|
string_list_t *replacements);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_define_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *macro,
|
2010-05-14 10:12:21 -07:00
|
|
|
string_list_t *parameters,
|
|
|
|
|
string_list_t *replacements);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
2010-05-14 10:31:43 -07:00
|
|
|
string_list_t *
|
|
|
|
|
_expand_object_macro (glcpp_parser_t *parser, const char *identifier);
|
|
|
|
|
|
|
|
|
|
string_list_t *
|
|
|
|
|
_expand_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *identifier,
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *arguments);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
|
|
|
|
void
|
2010-05-14 10:31:43 -07:00
|
|
|
_print_string_list (string_list_t *list);
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-14 10:05:11 -07:00
|
|
|
string_list_t *
|
|
|
|
|
_string_list_create (void *ctx);
|
2010-05-12 12:17:10 -07:00
|
|
|
|
|
|
|
|
void
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_append_item (string_list_t *list, const char *str);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
|
|
|
|
void
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_append_list (string_list_t *list, string_list_t *tail);
|
2010-05-11 12:30:09 -07:00
|
|
|
|
2010-05-13 12:56:42 -07:00
|
|
|
int
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_contains (string_list_t *list, const char *member, int *index);
|
2010-05-13 12:56:42 -07:00
|
|
|
|
|
|
|
|
int
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_length (string_list_t *list);
|
2010-05-13 12:56:42 -07:00
|
|
|
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *
|
|
|
|
|
_argument_list_create (void *ctx);
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_argument_list_append (argument_list_t *list, string_list_t *argument);
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
_argument_list_length (argument_list_t *list);
|
|
|
|
|
|
|
|
|
|
string_list_t *
|
|
|
|
|
_argument_list_member_at (argument_list_t *list, int index);
|
|
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
%}
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
%union {
|
|
|
|
|
char *str;
|
2010-05-14 10:44:19 -07:00
|
|
|
string_list_t *string_list;
|
|
|
|
|
argument_list_t *argument_list;
|
2010-05-12 12:17:10 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-10 16:16:06 -07:00
|
|
|
%parse-param {glcpp_parser_t *parser}
|
2010-05-10 11:52:29 -07:00
|
|
|
%lex-param {void *scanner}
|
|
|
|
|
|
2010-05-13 10:29:07 -07:00
|
|
|
%token DEFINE FUNC_MACRO IDENTIFIER NEWLINE OBJ_MACRO SPACE TOKEN UNDEF
|
2010-05-14 12:05:37 -07:00
|
|
|
%type <str> FUNC_MACRO IDENTIFIER identifier_perhaps_macro OBJ_MACRO replacement_word TOKEN word
|
2010-05-14 10:44:19 -07:00
|
|
|
%type <string_list> argument macro parameter_list replacement_list
|
|
|
|
|
%type <argument_list> argument_list
|
2010-05-10 11:44:09 -07:00
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
input:
|
|
|
|
|
/* empty */
|
2010-05-12 13:21:20 -07:00
|
|
|
| input content
|
2010-05-10 11:44:09 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-14 10:17:38 -07:00
|
|
|
/* We do all printing at the content level */
|
2010-05-12 12:17:10 -07:00
|
|
|
content:
|
2010-05-13 07:38:29 -07:00
|
|
|
IDENTIFIER {
|
|
|
|
|
printf ("%s", $1);
|
|
|
|
|
talloc_free ($1);
|
|
|
|
|
}
|
|
|
|
|
| TOKEN {
|
|
|
|
|
printf ("%s", $1);
|
|
|
|
|
talloc_free ($1);
|
|
|
|
|
}
|
2010-05-14 10:31:43 -07:00
|
|
|
| macro {
|
|
|
|
|
_print_string_list ($1);
|
|
|
|
|
}
|
2010-05-14 10:17:38 -07:00
|
|
|
| directive_with_newline { printf ("\n"); }
|
2010-05-13 10:29:07 -07:00
|
|
|
| NEWLINE { printf ("\n"); }
|
|
|
|
|
| '(' { printf ("("); }
|
|
|
|
|
| ')' { printf (")"); }
|
|
|
|
|
| ',' { printf (","); }
|
2010-05-12 13:11:50 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
macro:
|
|
|
|
|
FUNC_MACRO '(' argument_list ')' {
|
2010-05-14 10:31:43 -07:00
|
|
|
$$ = _expand_function_macro (parser, $1, $3);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
|
|
|
|
| OBJ_MACRO {
|
2010-05-14 10:31:43 -07:00
|
|
|
$$ = _expand_object_macro (parser, $1);
|
2010-05-13 09:36:23 -07:00
|
|
|
talloc_free ($1);
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
argument_list:
|
2010-05-14 11:33:00 -07:00
|
|
|
/* empty */ {
|
|
|
|
|
$$ = _argument_list_create (parser);
|
|
|
|
|
}
|
|
|
|
|
| argument {
|
2010-05-14 10:44:19 -07:00
|
|
|
$$ = _argument_list_create (parser);
|
|
|
|
|
_argument_list_append ($$, $1);
|
|
|
|
|
}
|
2010-05-13 09:36:23 -07:00
|
|
|
| argument_list ',' argument {
|
2010-05-14 10:44:19 -07:00
|
|
|
_argument_list_append ($1, $3);
|
2010-05-13 09:36:23 -07:00
|
|
|
$$ = $1;
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
argument:
|
2010-05-14 11:33:00 -07:00
|
|
|
word {
|
2010-05-14 10:05:11 -07:00
|
|
|
$$ = _string_list_create (parser);
|
2010-05-14 11:33:00 -07:00
|
|
|
_string_list_append_item ($$, $1);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
2010-05-14 12:05:37 -07:00
|
|
|
| macro {
|
|
|
|
|
$$ = $1;
|
|
|
|
|
}
|
2010-05-13 10:29:07 -07:00
|
|
|
| argument word {
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_append_item ($1, $2);
|
2010-05-13 09:36:23 -07:00
|
|
|
talloc_free ($2);
|
2010-05-14 16:53:52 -07:00
|
|
|
$$ = $1;
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
2010-05-14 16:53:52 -07:00
|
|
|
| argument '(' argument ')' {
|
|
|
|
|
_string_list_append_item ($1, "(");
|
|
|
|
|
_string_list_append_list ($1, $3);
|
|
|
|
|
_string_list_append_item ($1, ")");
|
|
|
|
|
$$ = $1;
|
2010-05-14 10:44:19 -07:00
|
|
|
}
|
2010-05-13 09:36:23 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-12 13:11:50 -07:00
|
|
|
directive_with_newline:
|
2010-05-14 10:17:38 -07:00
|
|
|
directive NEWLINE
|
2010-05-10 16:16:06 -07:00
|
|
|
;
|
2010-05-10 11:44:09 -07:00
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
directive:
|
2010-05-13 10:29:07 -07:00
|
|
|
DEFINE IDENTIFIER {
|
2010-05-14 10:05:11 -07:00
|
|
|
string_list_t *list = _string_list_create (parser);
|
2010-05-13 10:29:07 -07:00
|
|
|
_define_object_macro (parser, $2, list);
|
|
|
|
|
}
|
|
|
|
|
| DEFINE IDENTIFIER SPACE replacement_list {
|
|
|
|
|
_define_object_macro (parser, $2, $4);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
2010-05-13 10:46:29 -07:00
|
|
|
| DEFINE IDENTIFIER '(' parameter_list ')' {
|
2010-05-14 10:05:11 -07:00
|
|
|
string_list_t *list = _string_list_create (parser);
|
2010-05-13 10:46:29 -07:00
|
|
|
_define_function_macro (parser, $2, $4, list);
|
|
|
|
|
}
|
2010-05-14 17:08:45 -07:00
|
|
|
| DEFINE IDENTIFIER '(' parameter_list ')' replacement_list {
|
|
|
|
|
_define_function_macro (parser, $2, $4, $6);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
|
|
|
|
| UNDEF FUNC_MACRO {
|
2010-05-14 10:05:11 -07:00
|
|
|
string_list_t *replacement = hash_table_find (parser->defines, $2);
|
2010-05-13 09:36:23 -07:00
|
|
|
if (replacement) {
|
|
|
|
|
/* XXX: Need hash table to support a real way
|
|
|
|
|
* to remove an element rather than prefixing
|
|
|
|
|
* a new node with data of NULL like this. */
|
|
|
|
|
hash_table_insert (parser->defines, NULL, $2);
|
|
|
|
|
talloc_free (replacement);
|
|
|
|
|
}
|
|
|
|
|
talloc_free ($2);
|
2010-05-12 13:11:50 -07:00
|
|
|
}
|
2010-05-13 09:36:23 -07:00
|
|
|
| UNDEF OBJ_MACRO {
|
2010-05-14 10:05:11 -07:00
|
|
|
string_list_t *replacement = hash_table_find (parser->defines, $2);
|
2010-05-12 13:11:50 -07:00
|
|
|
if (replacement) {
|
|
|
|
|
/* XXX: Need hash table to support a real way
|
|
|
|
|
* to remove an element rather than prefixing
|
|
|
|
|
* a new node with data of NULL like this. */
|
|
|
|
|
hash_table_insert (parser->defines, NULL, $2);
|
|
|
|
|
talloc_free (replacement);
|
|
|
|
|
}
|
|
|
|
|
talloc_free ($2);
|
2010-05-12 12:17:10 -07:00
|
|
|
}
|
2010-05-10 11:44:09 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
replacement_list:
|
2010-05-14 12:05:37 -07:00
|
|
|
replacement_word {
|
2010-05-14 10:05:11 -07:00
|
|
|
$$ = _string_list_create (parser);
|
|
|
|
|
_string_list_append_item ($$, $1);
|
2010-05-13 10:46:29 -07:00
|
|
|
talloc_free ($1);
|
2010-05-12 12:17:10 -07:00
|
|
|
}
|
2010-05-14 12:05:37 -07:00
|
|
|
| replacement_list replacement_word {
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_append_item ($1, $2);
|
2010-05-12 12:45:33 -07:00
|
|
|
talloc_free ($2);
|
2010-05-12 12:17:10 -07:00
|
|
|
$$ = $1;
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
2010-05-14 12:05:37 -07:00
|
|
|
replacement_word:
|
|
|
|
|
word { $$ = $1; }
|
|
|
|
|
| FUNC_MACRO { $$ = $1; }
|
|
|
|
|
| OBJ_MACRO { $$ = $1; }
|
|
|
|
|
| '(' { $$ = xtalloc_strdup (parser, "("); }
|
|
|
|
|
| ')' { $$ = xtalloc_strdup (parser, ")"); }
|
|
|
|
|
| ',' { $$ = xtalloc_strdup (parser, ","); }
|
|
|
|
|
;
|
|
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
parameter_list:
|
|
|
|
|
/* empty */ {
|
2010-05-14 10:05:11 -07:00
|
|
|
$$ = _string_list_create (parser);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
2010-05-13 12:58:49 -07:00
|
|
|
| identifier_perhaps_macro {
|
2010-05-14 10:05:11 -07:00
|
|
|
$$ = _string_list_create (parser);
|
|
|
|
|
_string_list_append_item ($$, $1);
|
2010-05-13 09:36:23 -07:00
|
|
|
talloc_free ($1);
|
|
|
|
|
}
|
2010-05-13 12:58:49 -07:00
|
|
|
| parameter_list ',' identifier_perhaps_macro {
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_append_item ($1, $3);
|
2010-05-13 09:36:23 -07:00
|
|
|
talloc_free ($3);
|
|
|
|
|
$$ = $1;
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
2010-05-13 12:58:49 -07:00
|
|
|
identifier_perhaps_macro:
|
|
|
|
|
IDENTIFIER { $$ = $1; }
|
|
|
|
|
| FUNC_MACRO { $$ = $1; }
|
|
|
|
|
| OBJ_MACRO { $$ = $1; }
|
|
|
|
|
;
|
|
|
|
|
|
2010-05-13 10:29:07 -07:00
|
|
|
word:
|
2010-05-13 07:38:29 -07:00
|
|
|
IDENTIFIER { $$ = $1; }
|
|
|
|
|
| TOKEN { $$ = $1; }
|
2010-05-10 11:44:09 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
2010-05-14 10:05:11 -07:00
|
|
|
string_list_t *
|
|
|
|
|
_string_list_create (void *ctx)
|
2010-05-12 12:17:10 -07:00
|
|
|
{
|
2010-05-14 10:05:11 -07:00
|
|
|
string_list_t *list;
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-14 10:05:11 -07:00
|
|
|
list = xtalloc (ctx, string_list_t);
|
2010-05-12 12:17:10 -07:00
|
|
|
list->head = NULL;
|
|
|
|
|
list->tail = NULL;
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_append_list (string_list_t *list, string_list_t *tail)
|
2010-05-13 09:36:23 -07:00
|
|
|
{
|
|
|
|
|
if (list->head == NULL) {
|
|
|
|
|
list->head = tail->head;
|
|
|
|
|
} else {
|
|
|
|
|
list->tail->next = tail->head;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->tail = tail->tail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_append_item (string_list_t *list, const char *str)
|
2010-05-12 12:17:10 -07:00
|
|
|
{
|
2010-05-14 10:05:11 -07:00
|
|
|
string_node_t *node;
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-14 10:05:11 -07:00
|
|
|
node = xtalloc (list, string_node_t);
|
2010-05-12 12:45:33 -07:00
|
|
|
node->str = xtalloc_strdup (node, str);
|
2010-05-12 12:17:10 -07:00
|
|
|
|
|
|
|
|
node->next = NULL;
|
|
|
|
|
|
|
|
|
|
if (list->head == NULL) {
|
|
|
|
|
list->head = node;
|
|
|
|
|
} else {
|
|
|
|
|
list->tail->next = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->tail = node;
|
|
|
|
|
}
|
2010-05-13 12:56:42 -07:00
|
|
|
|
|
|
|
|
int
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_contains (string_list_t *list, const char *member, int *index)
|
2010-05-13 12:56:42 -07:00
|
|
|
{
|
2010-05-14 10:05:11 -07:00
|
|
|
string_node_t *node;
|
2010-05-13 12:56:42 -07:00
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
for (i = 0, node = list->head; node; i++, node = node->next) {
|
|
|
|
|
if (strcmp (node->str, member) == 0) {
|
|
|
|
|
*index = i;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_length (string_list_t *list)
|
2010-05-13 12:56:42 -07:00
|
|
|
{
|
|
|
|
|
int length = 0;
|
2010-05-14 10:05:11 -07:00
|
|
|
string_node_t *node;
|
2010-05-13 12:56:42 -07:00
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
for (node = list->head; node; node = node->next)
|
|
|
|
|
length++;
|
|
|
|
|
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-14 10:31:43 -07:00
|
|
|
void
|
|
|
|
|
_print_string_list (string_list_t *list)
|
|
|
|
|
{
|
|
|
|
|
string_node_t *node;
|
|
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-05-14 17:08:45 -07:00
|
|
|
for (node = list->head; node; node = node->next) {
|
2010-05-14 10:31:43 -07:00
|
|
|
printf ("%s", node->str);
|
2010-05-14 17:08:45 -07:00
|
|
|
if (node->next)
|
|
|
|
|
printf (" ");
|
|
|
|
|
}
|
2010-05-14 10:31:43 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *
|
|
|
|
|
_argument_list_create (void *ctx)
|
2010-05-13 12:56:42 -07:00
|
|
|
{
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *list;
|
|
|
|
|
|
|
|
|
|
list = xtalloc (ctx, argument_list_t);
|
|
|
|
|
list->head = NULL;
|
|
|
|
|
list->tail = NULL;
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_argument_list_append (argument_list_t *list, string_list_t *argument)
|
|
|
|
|
{
|
|
|
|
|
argument_node_t *node;
|
|
|
|
|
|
|
|
|
|
if (argument == NULL || argument->head == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
node = xtalloc (list, argument_node_t);
|
|
|
|
|
node->argument = argument;
|
|
|
|
|
|
|
|
|
|
node->next = NULL;
|
|
|
|
|
|
|
|
|
|
if (list->head == NULL) {
|
|
|
|
|
list->head = node;
|
|
|
|
|
} else {
|
|
|
|
|
list->tail->next = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->tail = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
_argument_list_length (argument_list_t *list)
|
|
|
|
|
{
|
|
|
|
|
int length = 0;
|
|
|
|
|
argument_node_t *node;
|
|
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
for (node = list->head; node; node = node->next)
|
|
|
|
|
length++;
|
|
|
|
|
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string_list_t *
|
|
|
|
|
_argument_list_member_at (argument_list_t *list, int index)
|
|
|
|
|
{
|
|
|
|
|
argument_node_t *node;
|
2010-05-13 12:56:42 -07:00
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
node = list->head;
|
|
|
|
|
for (i = 0; i < index; i++) {
|
|
|
|
|
node = node->next;
|
|
|
|
|
if (node == NULL)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node)
|
2010-05-14 10:44:19 -07:00
|
|
|
return node->argument;
|
2010-05-13 12:56:42 -07:00
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
void
|
2010-05-10 13:17:25 -07:00
|
|
|
yyerror (void *scanner, const char *error)
|
2010-05-10 11:44:09 -07:00
|
|
|
{
|
|
|
|
|
fprintf (stderr, "Parse error: %s\n", error);
|
|
|
|
|
}
|
2010-05-10 16:16:06 -07:00
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
glcpp_parser_t *
|
|
|
|
|
glcpp_parser_create (void)
|
2010-05-10 16:16:06 -07:00
|
|
|
{
|
2010-05-12 12:17:10 -07:00
|
|
|
glcpp_parser_t *parser;
|
|
|
|
|
|
2010-05-12 12:45:33 -07:00
|
|
|
parser = xtalloc (NULL, glcpp_parser_t);
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-12 12:45:33 -07:00
|
|
|
yylex_init_extra (parser, &parser->scanner);
|
2010-05-10 16:16:06 -07:00
|
|
|
parser->defines = hash_table_ctor (32, hash_table_string_hash,
|
|
|
|
|
hash_table_string_compare);
|
2010-05-12 12:17:10 -07:00
|
|
|
|
|
|
|
|
return parser;
|
2010-05-10 16:16:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
glcpp_parser_parse (glcpp_parser_t *parser)
|
|
|
|
|
{
|
|
|
|
|
return yyparse (parser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-12 12:17:10 -07:00
|
|
|
glcpp_parser_destroy (glcpp_parser_t *parser)
|
2010-05-10 16:16:06 -07:00
|
|
|
{
|
|
|
|
|
yylex_destroy (parser->scanner);
|
|
|
|
|
hash_table_dtor (parser->defines);
|
2010-05-12 12:17:10 -07:00
|
|
|
talloc_free (parser);
|
2010-05-10 16:16:06 -07:00
|
|
|
}
|
2010-05-11 12:30:09 -07:00
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
macro_type_t
|
|
|
|
|
glcpp_parser_macro_type (glcpp_parser_t *parser, const char *identifier)
|
2010-05-13 07:38:29 -07:00
|
|
|
{
|
2010-05-13 09:36:23 -07:00
|
|
|
macro_t *macro;
|
|
|
|
|
|
|
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
|
|
|
|
|
if (macro == NULL)
|
|
|
|
|
return MACRO_TYPE_UNDEFINED;
|
|
|
|
|
|
|
|
|
|
if (macro->is_function)
|
|
|
|
|
return MACRO_TYPE_FUNCTION;
|
|
|
|
|
else
|
|
|
|
|
return MACRO_TYPE_OBJECT;
|
2010-05-13 07:38:29 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
void
|
2010-05-13 09:36:23 -07:00
|
|
|
_define_object_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *identifier,
|
2010-05-14 10:12:21 -07:00
|
|
|
string_list_t *replacements)
|
2010-05-13 09:36:23 -07:00
|
|
|
{
|
|
|
|
|
macro_t *macro;
|
|
|
|
|
|
|
|
|
|
macro = xtalloc (parser, macro_t);
|
|
|
|
|
|
|
|
|
|
macro->is_function = 0;
|
2010-05-14 10:12:21 -07:00
|
|
|
macro->parameters = NULL;
|
|
|
|
|
macro->replacements = talloc_steal (macro, replacements);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
|
|
|
|
hash_table_insert (parser->defines, macro, identifier);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_define_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *identifier,
|
2010-05-14 10:12:21 -07:00
|
|
|
string_list_t *parameters,
|
|
|
|
|
string_list_t *replacements)
|
2010-05-13 09:36:23 -07:00
|
|
|
{
|
|
|
|
|
macro_t *macro;
|
|
|
|
|
|
|
|
|
|
macro = xtalloc (parser, macro_t);
|
|
|
|
|
|
|
|
|
|
macro->is_function = 1;
|
2010-05-14 10:12:21 -07:00
|
|
|
macro->parameters = talloc_steal (macro, parameters);
|
|
|
|
|
macro->replacements = talloc_steal (macro, replacements);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
|
|
|
|
hash_table_insert (parser->defines, macro, identifier);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-14 10:31:43 -07:00
|
|
|
static string_list_t *
|
|
|
|
|
_expand_macro_recursive (glcpp_parser_t *parser,
|
|
|
|
|
const char *token,
|
|
|
|
|
const char *orig,
|
|
|
|
|
string_list_t *parameters,
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *arguments);
|
2010-05-14 10:31:43 -07:00
|
|
|
|
|
|
|
|
static string_list_t *
|
|
|
|
|
_expand_string_list_recursive (glcpp_parser_t *parser,
|
|
|
|
|
string_list_t *list,
|
|
|
|
|
const char *orig,
|
|
|
|
|
string_list_t *parameters,
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *arguments)
|
2010-05-13 12:56:42 -07:00
|
|
|
{
|
2010-05-14 10:31:43 -07:00
|
|
|
string_list_t *result;
|
|
|
|
|
string_list_t *child;
|
2010-05-13 12:56:42 -07:00
|
|
|
const char *token;
|
2010-05-14 10:05:11 -07:00
|
|
|
string_node_t *node;
|
2010-05-13 12:56:42 -07:00
|
|
|
int index;
|
|
|
|
|
|
2010-05-14 10:31:43 -07:00
|
|
|
result = _string_list_create (parser);
|
|
|
|
|
|
2010-05-13 12:56:42 -07:00
|
|
|
for (node = list->head ; node ; node = node->next) {
|
|
|
|
|
token = node->str;
|
|
|
|
|
|
|
|
|
|
if (strcmp (token, orig) == 0) {
|
2010-05-14 10:31:43 -07:00
|
|
|
_string_list_append_item (result, token);
|
2010-05-13 12:56:42 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-14 10:05:11 -07:00
|
|
|
if (_string_list_contains (parameters, token, &index)) {
|
2010-05-14 10:44:19 -07:00
|
|
|
string_list_t *argument;
|
2010-05-13 12:56:42 -07:00
|
|
|
|
2010-05-14 10:44:19 -07:00
|
|
|
argument = _argument_list_member_at (arguments, index);
|
|
|
|
|
child = _expand_string_list_recursive (parser, argument,
|
|
|
|
|
orig, NULL, NULL);
|
2010-05-14 10:31:43 -07:00
|
|
|
_string_list_append_list (result, child);
|
2010-05-13 12:56:42 -07:00
|
|
|
} else {
|
2010-05-14 10:31:43 -07:00
|
|
|
child = _expand_macro_recursive (parser, token,
|
2010-05-13 12:56:42 -07:00
|
|
|
orig, parameters,
|
|
|
|
|
arguments);
|
2010-05-14 10:31:43 -07:00
|
|
|
_string_list_append_list (result, child);
|
2010-05-13 12:56:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
2010-05-14 10:31:43 -07:00
|
|
|
|
|
|
|
|
return result;
|
2010-05-13 12:56:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-05-14 10:31:43 -07:00
|
|
|
static string_list_t *
|
|
|
|
|
_expand_macro_recursive (glcpp_parser_t *parser,
|
|
|
|
|
const char *token,
|
|
|
|
|
const char *orig,
|
|
|
|
|
string_list_t *parameters,
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *arguments)
|
2010-05-13 12:56:42 -07:00
|
|
|
{
|
|
|
|
|
macro_t *macro;
|
2010-05-14 10:12:21 -07:00
|
|
|
string_list_t *replacements;
|
2010-05-13 12:56:42 -07:00
|
|
|
|
|
|
|
|
macro = hash_table_find (parser->defines, token);
|
|
|
|
|
if (macro == NULL) {
|
2010-05-14 10:31:43 -07:00
|
|
|
string_list_t *result;
|
|
|
|
|
|
|
|
|
|
result = _string_list_create (parser);
|
|
|
|
|
_string_list_append_item (result, token);
|
|
|
|
|
return result;
|
2010-05-13 12:56:42 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-14 10:12:21 -07:00
|
|
|
replacements = macro->replacements;
|
2010-05-13 12:56:42 -07:00
|
|
|
|
2010-05-14 10:31:43 -07:00
|
|
|
return _expand_string_list_recursive (parser, replacements,
|
|
|
|
|
orig, parameters, arguments);
|
2010-05-13 12:56:42 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-14 10:31:43 -07:00
|
|
|
string_list_t *
|
|
|
|
|
_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
|
2010-05-12 12:17:10 -07:00
|
|
|
{
|
2010-05-13 09:36:23 -07:00
|
|
|
macro_t *macro;
|
|
|
|
|
|
|
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
assert (! macro->is_function);
|
|
|
|
|
|
2010-05-14 10:31:43 -07:00
|
|
|
return _expand_macro_recursive (parser, identifier, identifier,
|
|
|
|
|
NULL, NULL);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-14 10:31:43 -07:00
|
|
|
string_list_t *
|
|
|
|
|
_expand_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *identifier,
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *arguments)
|
2010-05-13 09:36:23 -07:00
|
|
|
{
|
2010-05-14 10:31:43 -07:00
|
|
|
string_list_t *result;
|
2010-05-14 17:08:45 -07:00
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
macro_t *macro;
|
|
|
|
|
|
2010-05-14 10:31:43 -07:00
|
|
|
result = _string_list_create (parser);
|
|
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
assert (macro->is_function);
|
|
|
|
|
|
2010-05-14 10:44:19 -07:00
|
|
|
if (_argument_list_length (arguments) !=
|
2010-05-14 10:31:43 -07:00
|
|
|
_string_list_length (macro->parameters))
|
|
|
|
|
{
|
2010-05-13 12:56:42 -07:00
|
|
|
fprintf (stderr,
|
|
|
|
|
"Error: macro %s invoked with %d arguments (expected %d)\n",
|
|
|
|
|
identifier,
|
2010-05-14 10:44:19 -07:00
|
|
|
_argument_list_length (arguments),
|
2010-05-14 10:12:21 -07:00
|
|
|
_string_list_length (macro->parameters));
|
2010-05-14 10:31:43 -07:00
|
|
|
return NULL;
|
2010-05-13 12:56:42 -07:00
|
|
|
}
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-14 10:31:43 -07:00
|
|
|
return _expand_macro_recursive (parser, identifier, identifier,
|
|
|
|
|
macro->parameters, arguments);
|
2010-05-12 12:17:10 -07:00
|
|
|
}
|