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-10 11:44:09 -07:00
|
|
|
|
2010-05-10 13:17:25 -07:00
|
|
|
#include "glcpp.h"
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static 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-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-13 09:36:23 -07:00
|
|
|
_define_object_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *macro,
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *replacements);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-13 09:36:23 -07:00
|
|
|
_define_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *macro,
|
2010-05-14 10:12:21 -07:00
|
|
|
string_list_t *parameters,
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *replacements);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static string_list_t *
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_create (void *ctx);
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static 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
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static 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-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-25 15:24:59 -07:00
|
|
|
_string_list_push (string_list_t *list, const char *str);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-25 15:24:59 -07:00
|
|
|
_string_list_pop (string_list_t *list);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static 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-25 18:39:43 -07:00
|
|
|
static 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-25 18:39:43 -07:00
|
|
|
static argument_list_t *
|
2010-05-14 10:44:19 -07:00
|
|
|
_argument_list_create (void *ctx);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-19 13:54:37 -07:00
|
|
|
_argument_list_append (argument_list_t *list, token_list_t *argument);
|
2010-05-14 10:44:19 -07:00
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static int
|
2010-05-14 10:44:19 -07:00
|
|
|
_argument_list_length (argument_list_t *list);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static token_list_t *
|
2010-05-14 10:44:19 -07:00
|
|
|
_argument_list_member_at (argument_list_t *list, int index);
|
|
|
|
|
|
2010-05-25 14:52:43 -07:00
|
|
|
/* Note: This function talloc_steal()s the str pointer. */
|
2010-05-25 18:39:43 -07:00
|
|
|
static token_t *
|
2010-05-25 14:52:43 -07:00
|
|
|
_token_create_str (void *ctx, int type, char *str);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static token_t *
|
2010-05-25 14:52:43 -07:00
|
|
|
_token_create_ival (void *ctx, int type, int ival);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static token_list_t *
|
2010-05-19 13:54:37 -07:00
|
|
|
_token_list_create (void *ctx);
|
|
|
|
|
|
2010-05-25 14:52:43 -07:00
|
|
|
/* Note: This function add a talloc_reference() to token.
|
|
|
|
|
*
|
|
|
|
|
* You may want to talloc_unlink any current reference if you no
|
|
|
|
|
* longer need it. */
|
2010-05-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-25 14:52:43 -07:00
|
|
|
_token_list_append (token_list_t *list, token_t *token);
|
2010-05-19 13:54:37 -07:00
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-19 13:54:37 -07:00
|
|
|
_token_list_append_list (token_list_t *list, token_list_t *tail);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-25 15:24:59 -07:00
|
|
|
_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
|
|
|
|
|
token_list_t *list);
|
2010-05-25 14:52:43 -07:00
|
|
|
|
2010-05-20 22:27:07 -07:00
|
|
|
static void
|
|
|
|
|
_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition);
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
|
|
|
|
|
int condition);
|
2010-05-25 14:42:00 -07:00
|
|
|
|
2010-05-20 22:27:07 -07:00
|
|
|
static void
|
|
|
|
|
_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser);
|
|
|
|
|
|
2010-05-19 10:05:40 -07:00
|
|
|
#define yylex glcpp_parser_lex
|
|
|
|
|
|
2010-05-19 10:01:29 -07:00
|
|
|
static int
|
2010-05-19 10:05:40 -07:00
|
|
|
glcpp_parser_lex (glcpp_parser_t *parser);
|
2010-05-19 10:01:29 -07:00
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
%}
|
|
|
|
|
|
2010-05-10 16:16:06 -07:00
|
|
|
%parse-param {glcpp_parser_t *parser}
|
2010-05-19 10:05:40 -07:00
|
|
|
%lex-param {glcpp_parser_t *parser}
|
2010-05-10 11:52:29 -07:00
|
|
|
|
2010-05-25 16:28:26 -07:00
|
|
|
%token HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH_UNDEF IDENTIFIER NEWLINE OTHER SPACE
|
2010-05-25 13:09:03 -07:00
|
|
|
%token LEFT_SHIFT RIGHT_SHIFT LESS_OR_EQUAL GREATER_OR_EQUAL EQUAL NOT_EQUAL AND OR PASTE
|
2010-05-25 17:08:07 -07:00
|
|
|
%type <ival> punctuator SPACE
|
|
|
|
|
%type <str> IDENTIFIER OTHER
|
2010-05-25 16:28:26 -07:00
|
|
|
%type <string_list> identifier_list
|
2010-05-25 14:52:43 -07:00
|
|
|
%type <token> preprocessing_token
|
|
|
|
|
%type <token_list> pp_tokens replacement_list text_line
|
2010-05-25 13:09:03 -07:00
|
|
|
|
|
|
|
|
/* Stale stuff just to allow code to compile. */
|
|
|
|
|
%token IDENTIFIER_FINALIZED FUNC_MACRO OBJ_MACRO
|
2010-05-17 12:45:16 -07:00
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
%%
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
input:
|
2010-05-25 13:09:03 -07:00
|
|
|
/* empty */
|
2010-05-25 15:24:59 -07:00
|
|
|
| input line {
|
|
|
|
|
printf ("\n");
|
|
|
|
|
}
|
2010-05-10 11:44:09 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
line:
|
|
|
|
|
control_line
|
2010-05-25 14:52:43 -07:00
|
|
|
| text_line {
|
2010-05-25 15:24:59 -07:00
|
|
|
_glcpp_parser_print_expanded_token_list (parser, $1);
|
2010-05-25 14:52:43 -07:00
|
|
|
talloc_free ($1);
|
|
|
|
|
}
|
2010-05-25 13:09:03 -07:00
|
|
|
| HASH non_directive
|
2010-05-12 13:11:50 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
control_line:
|
2010-05-25 16:59:02 -07:00
|
|
|
HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
|
2010-05-25 15:24:59 -07:00
|
|
|
_define_object_macro (parser, $2, $3);
|
|
|
|
|
}
|
2010-05-25 16:28:26 -07:00
|
|
|
| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
|
|
|
|
|
_define_function_macro (parser, $2, NULL, $5);
|
|
|
|
|
}
|
|
|
|
|
| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
|
|
|
|
|
_define_function_macro (parser, $2, $4, $6);
|
|
|
|
|
}
|
2010-05-25 15:28:58 -07:00
|
|
|
| HASH_UNDEF IDENTIFIER NEWLINE {
|
|
|
|
|
string_list_t *macro = hash_table_find (parser->defines, $2);
|
|
|
|
|
if (macro) {
|
|
|
|
|
/* 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 (macro);
|
|
|
|
|
}
|
|
|
|
|
talloc_free ($2);
|
|
|
|
|
}
|
2010-05-25 13:09:03 -07:00
|
|
|
| HASH NEWLINE
|
2010-05-13 09:36:23 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
identifier_list:
|
2010-05-25 16:28:26 -07:00
|
|
|
IDENTIFIER {
|
|
|
|
|
$$ = _string_list_create (parser);
|
|
|
|
|
_string_list_append_item ($$, $1);
|
|
|
|
|
talloc_steal ($$, $1);
|
|
|
|
|
}
|
|
|
|
|
| identifier_list ',' IDENTIFIER {
|
|
|
|
|
$$ = $1;
|
|
|
|
|
_string_list_append_item ($$, $3);
|
|
|
|
|
talloc_steal ($$, $3);
|
|
|
|
|
}
|
2010-05-13 09:36:23 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
text_line:
|
2010-05-25 14:52:43 -07:00
|
|
|
NEWLINE { $$ = NULL; }
|
2010-05-25 13:09:03 -07:00
|
|
|
| pp_tokens NEWLINE
|
2010-05-19 07:49:47 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
non_directive:
|
|
|
|
|
pp_tokens NEWLINE
|
2010-05-20 08:42:02 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
replacement_list:
|
2010-05-25 14:52:43 -07:00
|
|
|
/* empty */ { $$ = NULL; }
|
2010-05-25 13:09:03 -07:00
|
|
|
| pp_tokens
|
2010-05-10 11:44:09 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
pp_tokens:
|
2010-05-25 14:52:43 -07:00
|
|
|
preprocessing_token {
|
2010-05-25 16:59:02 -07:00
|
|
|
parser->space_tokens = 1;
|
2010-05-25 14:52:43 -07:00
|
|
|
$$ = _token_list_create (parser);
|
|
|
|
|
_token_list_append ($$, $1);
|
|
|
|
|
talloc_unlink (parser, $1);
|
|
|
|
|
}
|
|
|
|
|
| pp_tokens preprocessing_token {
|
|
|
|
|
$$ = $1;
|
|
|
|
|
_token_list_append ($$, $2);
|
|
|
|
|
talloc_unlink (parser, $2);
|
|
|
|
|
}
|
2010-05-20 22:27:07 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
preprocessing_token:
|
2010-05-25 14:52:43 -07:00
|
|
|
IDENTIFIER {
|
|
|
|
|
$$ = _token_create_str (parser, IDENTIFIER, $1);
|
|
|
|
|
}
|
|
|
|
|
| punctuator {
|
|
|
|
|
$$ = _token_create_ival (parser, $1, $1);
|
|
|
|
|
}
|
|
|
|
|
| OTHER {
|
|
|
|
|
$$ = _token_create_str (parser, OTHER, $1);
|
|
|
|
|
}
|
2010-05-25 16:28:26 -07:00
|
|
|
| SPACE {
|
2010-05-25 17:08:07 -07:00
|
|
|
$$ = _token_create_ival (parser, SPACE, SPACE);
|
2010-05-25 16:28:26 -07:00
|
|
|
}
|
2010-05-13 09:36:23 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
punctuator:
|
2010-05-25 14:52:43 -07:00
|
|
|
'[' { $$ = '['; }
|
|
|
|
|
| ']' { $$ = ']'; }
|
|
|
|
|
| '(' { $$ = '('; }
|
|
|
|
|
| ')' { $$ = ')'; }
|
|
|
|
|
| '{' { $$ = '{'; }
|
|
|
|
|
| '}' { $$ = '}'; }
|
|
|
|
|
| '.' { $$ = '.'; }
|
|
|
|
|
| '&' { $$ = '&'; }
|
|
|
|
|
| '*' { $$ = '*'; }
|
|
|
|
|
| '+' { $$ = '+'; }
|
|
|
|
|
| '-' { $$ = '-'; }
|
|
|
|
|
| '~' { $$ = '~'; }
|
|
|
|
|
| '!' { $$ = '!'; }
|
|
|
|
|
| '/' { $$ = '/'; }
|
|
|
|
|
| '%' { $$ = '%'; }
|
|
|
|
|
| LEFT_SHIFT { $$ = LEFT_SHIFT; }
|
|
|
|
|
| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
|
|
|
|
|
| '<' { $$ = '<'; }
|
|
|
|
|
| '>' { $$ = '>'; }
|
|
|
|
|
| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
|
|
|
|
|
| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
|
|
|
|
|
| EQUAL { $$ = EQUAL; }
|
|
|
|
|
| NOT_EQUAL { $$ = NOT_EQUAL; }
|
|
|
|
|
| '^' { $$ = '^'; }
|
|
|
|
|
| '|' { $$ = '|'; }
|
|
|
|
|
| AND { $$ = AND; }
|
|
|
|
|
| OR { $$ = OR; }
|
|
|
|
|
| ';' { $$ = ';'; }
|
|
|
|
|
| ',' { $$ = ','; }
|
|
|
|
|
| PASTE { $$ = PASTE; }
|
2010-05-19 13:28:24 -07:00
|
|
|
;
|
|
|
|
|
|
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-25 14:42:00 -07:00
|
|
|
|
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
|
|
|
|
2010-05-25 15:24:59 -07:00
|
|
|
void
|
|
|
|
|
_string_list_push (string_list_t *list, const char *str)
|
|
|
|
|
{
|
|
|
|
|
string_node_t *node;
|
|
|
|
|
|
|
|
|
|
node = xtalloc (list, string_node_t);
|
|
|
|
|
node->str = xtalloc_strdup (node, str);
|
|
|
|
|
node->next = list->head;
|
|
|
|
|
|
|
|
|
|
if (list->tail == NULL) {
|
|
|
|
|
list->tail = node;
|
|
|
|
|
}
|
|
|
|
|
list->head = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_string_list_pop (string_list_t *list)
|
|
|
|
|
{
|
|
|
|
|
string_node_t *node;
|
|
|
|
|
|
|
|
|
|
node = list->head;
|
|
|
|
|
|
|
|
|
|
if (node == NULL) {
|
|
|
|
|
fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->head = node->next;
|
|
|
|
|
if (list->tail == node) {
|
|
|
|
|
assert (node->next == NULL);
|
|
|
|
|
list->tail = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
talloc_free (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) {
|
2010-05-17 10:15:23 -07:00
|
|
|
if (index)
|
|
|
|
|
*index = i;
|
2010-05-13 12:56:42 -07:00
|
|
|
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: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
|
2010-05-19 13:54:37 -07:00
|
|
|
_argument_list_append (argument_list_t *list, token_list_t *argument)
|
2010-05-14 10:44:19 -07:00
|
|
|
{
|
|
|
|
|
argument_node_t *node;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *
|
2010-05-14 10:44:19 -07:00
|
|
|
_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-19 13:54:37 -07:00
|
|
|
|
2010-05-25 14:52:43 -07:00
|
|
|
/* Note: This function talloc_steal()s the str pointer. */
|
|
|
|
|
token_t *
|
|
|
|
|
_token_create_str (void *ctx, int type, char *str)
|
|
|
|
|
{
|
|
|
|
|
token_t *token;
|
|
|
|
|
|
|
|
|
|
token = xtalloc (ctx, token_t);
|
|
|
|
|
token->type = type;
|
|
|
|
|
token->value.str = talloc_steal (token, str);
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token_t *
|
|
|
|
|
_token_create_ival (void *ctx, int type, int ival)
|
|
|
|
|
{
|
|
|
|
|
token_t *token;
|
|
|
|
|
|
|
|
|
|
token = xtalloc (ctx, token_t);
|
|
|
|
|
token->type = type;
|
|
|
|
|
token->value.ival = ival;
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-25 20:35:01 -07:00
|
|
|
_glcpp_parser_print_token (glcpp_parser_t *parser, token_t *token)
|
2010-05-25 14:52:43 -07:00
|
|
|
{
|
|
|
|
|
if (token->type < 256) {
|
|
|
|
|
printf ("%c", token->type);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (token->type) {
|
|
|
|
|
case IDENTIFIER:
|
|
|
|
|
case OTHER:
|
|
|
|
|
printf ("%s", token->value.str);
|
|
|
|
|
break;
|
2010-05-25 17:08:07 -07:00
|
|
|
case SPACE:
|
|
|
|
|
printf (" ");
|
|
|
|
|
break;
|
2010-05-25 14:52:43 -07:00
|
|
|
case LEFT_SHIFT:
|
|
|
|
|
printf ("<<");
|
|
|
|
|
break;
|
|
|
|
|
case RIGHT_SHIFT:
|
|
|
|
|
printf (">>");
|
|
|
|
|
break;
|
|
|
|
|
case LESS_OR_EQUAL:
|
|
|
|
|
printf ("<=");
|
|
|
|
|
break;
|
|
|
|
|
case GREATER_OR_EQUAL:
|
|
|
|
|
printf (">=");
|
|
|
|
|
break;
|
|
|
|
|
case EQUAL:
|
|
|
|
|
printf ("==");
|
|
|
|
|
break;
|
|
|
|
|
case NOT_EQUAL:
|
|
|
|
|
printf ("!=");
|
|
|
|
|
break;
|
|
|
|
|
case AND:
|
|
|
|
|
printf ("&&");
|
|
|
|
|
break;
|
|
|
|
|
case OR:
|
|
|
|
|
printf ("||");
|
|
|
|
|
break;
|
|
|
|
|
case PASTE:
|
|
|
|
|
printf ("##");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *
|
|
|
|
|
_token_list_create (void *ctx)
|
|
|
|
|
{
|
|
|
|
|
token_list_t *list;
|
|
|
|
|
|
|
|
|
|
list = xtalloc (ctx, token_list_t);
|
|
|
|
|
list->head = NULL;
|
|
|
|
|
list->tail = NULL;
|
2010-05-25 20:35:01 -07:00
|
|
|
list->non_space_tail = NULL;
|
2010-05-19 13:54:37 -07:00
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-25 14:52:43 -07:00
|
|
|
_token_list_append (token_list_t *list, token_t *token)
|
2010-05-19 13:54:37 -07:00
|
|
|
{
|
|
|
|
|
token_node_t *node;
|
|
|
|
|
|
|
|
|
|
node = xtalloc (list, token_node_t);
|
2010-05-25 14:52:43 -07:00
|
|
|
node->token = xtalloc_reference (list, token);
|
2010-05-19 13:54:37 -07:00
|
|
|
|
|
|
|
|
node->next = NULL;
|
|
|
|
|
|
|
|
|
|
if (list->head == NULL) {
|
|
|
|
|
list->head = node;
|
|
|
|
|
} else {
|
|
|
|
|
list->tail->next = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->tail = node;
|
2010-05-25 20:35:01 -07:00
|
|
|
if (token->type != SPACE)
|
|
|
|
|
list->non_space_tail = node;
|
2010-05-19 13:54:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_token_list_append_list (token_list_t *list, token_list_t *tail)
|
|
|
|
|
{
|
|
|
|
|
if (list->head == NULL) {
|
|
|
|
|
list->head = tail->head;
|
|
|
|
|
} else {
|
|
|
|
|
list->tail->next = tail->head;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->tail = tail->tail;
|
2010-05-25 20:35:01 -07:00
|
|
|
list->non_space_tail = tail->non_space_tail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_token_list_trim_trailing_space (token_list_t *list)
|
|
|
|
|
{
|
|
|
|
|
token_node_t *tail, *next;
|
|
|
|
|
|
|
|
|
|
if (list->non_space_tail) {
|
|
|
|
|
tail = list->non_space_tail->next;
|
|
|
|
|
list->non_space_tail->next = NULL;
|
|
|
|
|
list->tail = list->non_space_tail;
|
|
|
|
|
|
|
|
|
|
while (tail) {
|
|
|
|
|
next = tail->next;
|
|
|
|
|
talloc_free (tail);
|
|
|
|
|
tail = next;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-19 13:54:37 -07:00
|
|
|
}
|
2010-05-25 14:42:00 -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-19 10:01:29 -07:00
|
|
|
glcpp_lex_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-25 15:24:59 -07:00
|
|
|
parser->active = _string_list_create (parser);
|
2010-05-25 16:59:02 -07:00
|
|
|
parser->space_tokens = 1;
|
2010-05-20 14:29:43 -07:00
|
|
|
|
2010-05-20 22:27:07 -07:00
|
|
|
parser->skip_stack = NULL;
|
|
|
|
|
|
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
|
|
|
{
|
2010-05-20 22:27:07 -07:00
|
|
|
if (parser->skip_stack)
|
|
|
|
|
fprintf (stderr, "Error: Unterminated #if\n");
|
2010-05-19 10:01:29 -07:00
|
|
|
glcpp_lex_destroy (parser->scanner);
|
2010-05-10 16:16:06 -07:00
|
|
|
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-25 16:28:26 -07:00
|
|
|
/* Print a non-macro token, or the expansion of an object-like macro.
|
|
|
|
|
*
|
|
|
|
|
* Returns 0 if this token is completely printed.
|
|
|
|
|
*
|
|
|
|
|
* Returns 1 in the case that 'token' is a function-like macro that
|
|
|
|
|
* needs further expansion.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
2010-05-25 15:24:59 -07:00
|
|
|
_glcpp_parser_print_expanded_token (glcpp_parser_t *parser,
|
|
|
|
|
token_t *token)
|
|
|
|
|
{
|
|
|
|
|
const char *identifier;
|
|
|
|
|
macro_t *macro;
|
|
|
|
|
|
|
|
|
|
/* We only expand identifiers */
|
|
|
|
|
if (token->type != IDENTIFIER) {
|
2010-05-25 20:35:01 -07:00
|
|
|
_glcpp_parser_print_token (parser, token);
|
2010-05-25 16:28:26 -07:00
|
|
|
return 0;
|
2010-05-25 15:24:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Look up this identifier in the hash table. */
|
|
|
|
|
identifier = token->value.str;
|
|
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
|
|
|
|
|
/* Not a macro, so just print directly. */
|
|
|
|
|
if (macro == NULL) {
|
|
|
|
|
printf ("%s", identifier);
|
2010-05-25 16:28:26 -07:00
|
|
|
return 0;
|
2010-05-25 15:24:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Finally, don't expand this macro if we're already actively
|
|
|
|
|
* expanding it, (to avoid infinite recursion). */
|
|
|
|
|
if (_string_list_contains (parser->active, identifier, NULL)) {
|
2010-05-25 16:28:26 -07:00
|
|
|
printf ("%s", identifier);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 08:01:42 -07:00
|
|
|
/* For function-like macros return 1 for further processing. */
|
|
|
|
|
if (macro->is_function) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:28:26 -07:00
|
|
|
_string_list_push (parser->active, identifier);
|
|
|
|
|
_glcpp_parser_print_expanded_token_list (parser,
|
|
|
|
|
macro->replacements);
|
|
|
|
|
_string_list_pop (parser->active);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef enum function_status
|
|
|
|
|
{
|
|
|
|
|
FUNCTION_STATUS_SUCCESS,
|
|
|
|
|
FUNCTION_NOT_A_FUNCTION,
|
|
|
|
|
FUNCTION_UNBALANCED_PARENTHESES
|
|
|
|
|
} function_status_t;
|
|
|
|
|
|
|
|
|
|
/* Find a set of function-like macro arguments by looking for a
|
|
|
|
|
* balanced set of parentheses. Upon return *node will be the last
|
|
|
|
|
* consumed node, such that further processing can continue with
|
|
|
|
|
* node->next.
|
|
|
|
|
*
|
|
|
|
|
* Return values:
|
|
|
|
|
*
|
|
|
|
|
* FUNCTION_STATUS_SUCCESS:
|
|
|
|
|
*
|
|
|
|
|
* Successfully parsed a set of function arguments.
|
|
|
|
|
*
|
|
|
|
|
* FUNCTION_NOT_A_FUNCTION:
|
|
|
|
|
*
|
|
|
|
|
* Macro name not followed by a '('. This is not an error, but
|
|
|
|
|
* simply that the macro name should be treated as a non-macro.
|
|
|
|
|
*
|
|
|
|
|
* FUNCTION_UNBLANCED_PARENTHESES
|
|
|
|
|
*
|
|
|
|
|
* Macro name is not followed by a balanced set of parentheses.
|
|
|
|
|
*/
|
|
|
|
|
static function_status_t
|
2010-05-25 17:32:21 -07:00
|
|
|
_arguments_parse (argument_list_t *arguments, token_node_t **node_ret)
|
2010-05-25 16:28:26 -07:00
|
|
|
{
|
2010-05-25 17:32:21 -07:00
|
|
|
token_list_t *argument;
|
2010-05-25 16:28:26 -07:00
|
|
|
token_node_t *node = *node_ret, *last;
|
|
|
|
|
int paren_count;
|
|
|
|
|
|
|
|
|
|
last = node;
|
|
|
|
|
node = node->next;
|
|
|
|
|
|
|
|
|
|
/* Ignore whitespace before first parenthesis. */
|
|
|
|
|
while (node && node->token->type == SPACE)
|
|
|
|
|
node = node->next;
|
|
|
|
|
|
|
|
|
|
if (node == NULL || node->token->type != '(')
|
|
|
|
|
return FUNCTION_NOT_A_FUNCTION;
|
|
|
|
|
|
2010-05-25 17:45:22 -07:00
|
|
|
last = node;
|
|
|
|
|
node = node->next;
|
|
|
|
|
|
2010-05-25 17:32:21 -07:00
|
|
|
argument = NULL;
|
|
|
|
|
|
2010-05-25 17:45:22 -07:00
|
|
|
for (paren_count = 1; node; last = node, node = node->next) {
|
2010-05-25 16:28:26 -07:00
|
|
|
if (node->token->type == '(')
|
|
|
|
|
{
|
|
|
|
|
paren_count++;
|
|
|
|
|
}
|
|
|
|
|
else if (node->token->type == ')')
|
|
|
|
|
{
|
|
|
|
|
paren_count--;
|
2010-05-25 17:41:07 -07:00
|
|
|
if (paren_count == 0) {
|
|
|
|
|
last = node;
|
|
|
|
|
node = node->next;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-05-25 16:28:26 -07:00
|
|
|
}
|
2010-05-25 17:45:22 -07:00
|
|
|
|
|
|
|
|
if (node->token->type == ',' &&
|
2010-05-25 16:28:26 -07:00
|
|
|
paren_count == 1)
|
|
|
|
|
{
|
2010-05-25 20:35:01 -07:00
|
|
|
if (argument)
|
|
|
|
|
_token_list_trim_trailing_space (argument);
|
2010-05-25 17:32:21 -07:00
|
|
|
argument = NULL;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (argument == NULL) {
|
2010-05-25 17:41:07 -07:00
|
|
|
/* Don't treat initial whitespace as
|
|
|
|
|
* part of the arguement. */
|
|
|
|
|
if (node->token->type == SPACE)
|
|
|
|
|
continue;
|
2010-05-25 17:32:21 -07:00
|
|
|
argument = _token_list_create (arguments);
|
|
|
|
|
_argument_list_append (arguments, argument);
|
|
|
|
|
}
|
|
|
|
|
_token_list_append (argument, node->token);
|
2010-05-25 16:28:26 -07:00
|
|
|
}
|
2010-05-25 17:41:07 -07:00
|
|
|
}
|
2010-05-25 16:28:26 -07:00
|
|
|
|
|
|
|
|
if (node && paren_count)
|
|
|
|
|
return FUNCTION_UNBALANCED_PARENTHESES;
|
|
|
|
|
|
|
|
|
|
*node_ret = last;
|
|
|
|
|
|
|
|
|
|
return FUNCTION_STATUS_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Prints the expansion of *node (consuming further tokens from the
|
|
|
|
|
* list as necessary). Upon return *node will be the last consumed
|
|
|
|
|
* node, such that further processing can continue with node->next. */
|
|
|
|
|
static void
|
|
|
|
|
_glcpp_parser_print_expanded_function (glcpp_parser_t *parser,
|
|
|
|
|
token_node_t **node_ret)
|
|
|
|
|
{
|
|
|
|
|
macro_t *macro;
|
|
|
|
|
token_node_t *node;
|
|
|
|
|
const char *identifier;
|
|
|
|
|
argument_list_t *arguments;
|
|
|
|
|
function_status_t status;
|
2010-05-25 17:32:21 -07:00
|
|
|
token_list_t *expanded;
|
|
|
|
|
token_node_t *i, *j;
|
|
|
|
|
int parameter_index;
|
2010-05-25 16:28:26 -07:00
|
|
|
|
|
|
|
|
node = *node_ret;
|
|
|
|
|
identifier = node->token->value.str;
|
|
|
|
|
|
|
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
|
|
|
|
|
assert (macro->is_function);
|
|
|
|
|
|
2010-05-25 17:32:21 -07:00
|
|
|
arguments = _argument_list_create (parser);
|
|
|
|
|
status = _arguments_parse (arguments, node_ret);
|
2010-05-25 16:28:26 -07:00
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
|
case FUNCTION_STATUS_SUCCESS:
|
|
|
|
|
break;
|
|
|
|
|
case FUNCTION_NOT_A_FUNCTION:
|
2010-05-25 15:24:59 -07:00
|
|
|
printf ("%s", identifier);
|
|
|
|
|
return;
|
2010-05-25 16:28:26 -07:00
|
|
|
case FUNCTION_UNBALANCED_PARENTHESES:
|
|
|
|
|
fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n",
|
|
|
|
|
identifier);
|
|
|
|
|
exit (1);
|
2010-05-25 15:24:59 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 17:32:21 -07:00
|
|
|
if (macro->replacements == NULL) {
|
|
|
|
|
talloc_free (arguments);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (_argument_list_length (arguments) !=
|
|
|
|
|
_string_list_length (macro->parameters))
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr,
|
|
|
|
|
"Error: macro %s invoked with %d arguments (expected %d)\n",
|
|
|
|
|
identifier,
|
|
|
|
|
_argument_list_length (arguments),
|
|
|
|
|
_string_list_length (macro->parameters));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expanded = _token_list_create (arguments);
|
|
|
|
|
|
|
|
|
|
for (i = macro->replacements->head; i; i = i->next) {
|
|
|
|
|
if (i->token->type == IDENTIFIER &&
|
|
|
|
|
_string_list_contains (macro->parameters,
|
|
|
|
|
i->token->value.str,
|
|
|
|
|
¶meter_index))
|
|
|
|
|
{
|
|
|
|
|
token_list_t *argument;
|
|
|
|
|
argument = _argument_list_member_at (arguments,
|
|
|
|
|
parameter_index);
|
|
|
|
|
for (j = argument->head; j; j = j->next)
|
|
|
|
|
{
|
|
|
|
|
_token_list_append (expanded, j->token);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
_token_list_append (expanded, i->token);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 15:24:59 -07:00
|
|
|
_string_list_push (parser->active, identifier);
|
2010-05-25 17:32:21 -07:00
|
|
|
_glcpp_parser_print_expanded_token_list (parser, expanded);
|
2010-05-25 15:24:59 -07:00
|
|
|
_string_list_pop (parser->active);
|
2010-05-25 17:32:21 -07:00
|
|
|
|
|
|
|
|
talloc_free (arguments);
|
2010-05-25 15:24:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
|
|
|
|
|
token_list_t *list)
|
|
|
|
|
{
|
|
|
|
|
token_node_t *node;
|
2010-05-25 16:28:26 -07:00
|
|
|
function_status_t function_status;
|
2010-05-25 15:24:59 -07:00
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-05-25 20:35:01 -07:00
|
|
|
_token_list_trim_trailing_space (list);
|
|
|
|
|
|
2010-05-25 15:24:59 -07:00
|
|
|
for (node = list->head; node; node = node->next) {
|
2010-05-25 16:28:26 -07:00
|
|
|
if (_glcpp_parser_print_expanded_token (parser, node->token))
|
|
|
|
|
_glcpp_parser_print_expanded_function (parser, &node);
|
2010-05-25 15:24:59 -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-19 13:54:37 -07:00
|
|
|
token_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;
|
2010-05-18 22:10:04 -07:00
|
|
|
macro->identifier = talloc_strdup (macro, identifier);
|
2010-05-19 13:28:24 -07:00
|
|
|
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,
|
2010-05-19 13:54:37 -07:00
|
|
|
token_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);
|
2010-05-18 22:10:04 -07:00
|
|
|
macro->identifier = talloc_strdup (macro, identifier);
|
2010-05-19 13:28:24 -07:00
|
|
|
macro->replacements = talloc_steal (macro, replacements);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
|
|
|
|
hash_table_insert (parser->defines, macro, identifier);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-19 10:01:29 -07:00
|
|
|
static int
|
2010-05-19 10:05:40 -07:00
|
|
|
glcpp_parser_lex (glcpp_parser_t *parser)
|
2010-05-19 10:01:29 -07:00
|
|
|
{
|
2010-05-25 18:39:43 -07:00
|
|
|
return glcpp_lex (parser->scanner);
|
2010-05-19 10:01:29 -07:00
|
|
|
}
|
2010-05-20 22:27:07 -07:00
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
|
|
|
|
|
{
|
|
|
|
|
skip_type_t current = SKIP_NO_SKIP;
|
|
|
|
|
skip_node_t *node;
|
|
|
|
|
|
|
|
|
|
if (parser->skip_stack)
|
|
|
|
|
current = parser->skip_stack->type;
|
|
|
|
|
|
|
|
|
|
node = xtalloc (parser, skip_node_t);
|
|
|
|
|
|
|
|
|
|
if (current == SKIP_NO_SKIP) {
|
|
|
|
|
if (condition)
|
|
|
|
|
node->type = SKIP_NO_SKIP;
|
|
|
|
|
else
|
|
|
|
|
node->type = SKIP_TO_ELSE;
|
|
|
|
|
} else {
|
|
|
|
|
node->type = SKIP_TO_ENDIF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node->next = parser->skip_stack;
|
|
|
|
|
parser->skip_stack = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
|
|
|
|
|
int condition)
|
|
|
|
|
{
|
|
|
|
|
if (parser->skip_stack == NULL) {
|
|
|
|
|
fprintf (stderr, "Error: %s without #if\n", type);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parser->skip_stack->type == SKIP_TO_ELSE) {
|
|
|
|
|
if (condition)
|
|
|
|
|
parser->skip_stack->type = SKIP_NO_SKIP;
|
|
|
|
|
} else {
|
|
|
|
|
parser->skip_stack->type = SKIP_TO_ENDIF;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-25 14:42:00 -07:00
|
|
|
|
2010-05-20 22:27:07 -07:00
|
|
|
static void
|
|
|
|
|
_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
|
|
|
|
|
{
|
|
|
|
|
skip_node_t *node;
|
|
|
|
|
|
|
|
|
|
if (parser->skip_stack == NULL) {
|
|
|
|
|
fprintf (stderr, "Error: #endif without #if\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node = parser->skip_stack;
|
|
|
|
|
parser->skip_stack = node->next;
|
|
|
|
|
talloc_free (node);
|
|
|
|
|
}
|