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-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-19 13:54:37 -07:00
|
|
|
token_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,
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *replacements);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
void
|
2010-05-14 10:31:43 -07:00
|
|
|
_expand_object_macro (glcpp_parser_t *parser, const char *identifier);
|
|
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
void
|
2010-05-14 10:31:43 -07:00
|
|
|
_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: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
|
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
|
|
|
|
|
|
|
|
int
|
|
|
|
|
_argument_list_length (argument_list_t *list);
|
|
|
|
|
|
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);
|
|
|
|
|
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *
|
|
|
|
|
_token_list_create (void *ctx);
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_token_list_append (token_list_t *list, int type, const char *value);
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_token_list_append_list (token_list_t *list, token_list_t *tail);
|
|
|
|
|
|
2010-05-19 13:28:24 -07:00
|
|
|
static void
|
|
|
|
|
glcpp_parser_pop_expansion (glcpp_parser_t *parser);
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
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-12 12:17:10 -07:00
|
|
|
%union {
|
2010-05-20 14:19:57 -07:00
|
|
|
int ival;
|
2010-05-12 12:17:10 -07:00
|
|
|
char *str;
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *argument_list;
|
2010-05-19 13:54:37 -07:00
|
|
|
string_list_t *string_list;
|
2010-05-20 08:01:44 -07:00
|
|
|
token_t token;
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *token_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-19 10:05:40 -07:00
|
|
|
%lex-param {glcpp_parser_t *parser}
|
2010-05-10 11:52:29 -07:00
|
|
|
|
2010-05-24 10:37:38 -07:00
|
|
|
%token DEFINE DEFINED ELIF ELSE ENDIF FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED IF IFDEF IFNDEF INTEGER OBJ_MACRO NEWLINE SPACE TOKEN UNDEF
|
2010-05-20 22:27:07 -07:00
|
|
|
%type <ival> expression INTEGER punctuator
|
2010-05-20 14:19:57 -07:00
|
|
|
%type <str> content FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO
|
2010-05-14 10:44:19 -07:00
|
|
|
%type <argument_list> argument_list
|
2010-05-19 13:54:37 -07:00
|
|
|
%type <string_list> macro parameter_list
|
2010-05-20 08:42:02 -07:00
|
|
|
%type <token> TOKEN argument_word argument_word_or_comma
|
|
|
|
|
%type <token_list> argument argument_or_comma replacement_list pp_tokens
|
2010-05-24 10:37:38 -07:00
|
|
|
%left OR
|
|
|
|
|
%left AND
|
|
|
|
|
%left '|'
|
|
|
|
|
%left '^'
|
|
|
|
|
%left '&'
|
|
|
|
|
%left EQUAL NOT_EQUAL
|
|
|
|
|
%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
|
|
|
|
|
%left LEFT_SHIFT RIGHT_SHIFT
|
|
|
|
|
%left '+' '-'
|
|
|
|
|
%left '*' '/' '%'
|
|
|
|
|
%right UNARY
|
2010-05-10 11:44:09 -07:00
|
|
|
|
2010-05-17 12:45:16 -07:00
|
|
|
/* Hard to remove shift/reduce conflicts documented as follows:
|
|
|
|
|
*
|
|
|
|
|
* 1. '(' after FUNC_MACRO name which is correctly resolved to shift
|
|
|
|
|
* to form macro invocation rather than reducing directly to
|
|
|
|
|
* content.
|
2010-05-19 07:42:42 -07:00
|
|
|
*
|
|
|
|
|
* 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to
|
|
|
|
|
* shift to form macro invocation rather than reducing directly to
|
|
|
|
|
* argument.
|
2010-05-20 08:42:02 -07:00
|
|
|
*
|
|
|
|
|
* 3. Similarly again now that we added argument_or_comma as well.
|
2010-05-17 12:45:16 -07:00
|
|
|
*/
|
2010-05-20 08:42:02 -07:00
|
|
|
%expect 3
|
2010-05-17 12:45:16 -07:00
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
%%
|
|
|
|
|
|
2010-05-24 10:37:38 -07:00
|
|
|
/* We do all printing at the input level. */
|
2010-05-12 12:17:10 -07:00
|
|
|
input:
|
2010-05-20 14:19:57 -07:00
|
|
|
/* empty */ {
|
2010-05-20 14:29:43 -07:00
|
|
|
parser->just_printed_separator = 1;
|
2010-05-20 14:19:57 -07:00
|
|
|
}
|
|
|
|
|
| input content {
|
|
|
|
|
int is_token;
|
2010-05-20 22:27:07 -07:00
|
|
|
int skipping = 0;
|
|
|
|
|
|
|
|
|
|
if (parser->skip_stack && parser->skip_stack->type != SKIP_NO_SKIP)
|
|
|
|
|
skipping = 1;
|
2010-05-20 14:19:57 -07:00
|
|
|
|
2010-05-20 22:27:07 -07:00
|
|
|
if ($2 && strlen ($2) && ! skipping) {
|
2010-05-20 14:19:57 -07:00
|
|
|
int c = $2[0];
|
|
|
|
|
int is_not_separator = ((c >= 'a' && c <= 'z') ||
|
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
|
|
|
|
(c >= '0' && c <= '9') ||
|
|
|
|
|
(c == '_'));
|
|
|
|
|
|
2010-05-20 14:29:43 -07:00
|
|
|
if (! parser->just_printed_separator && is_not_separator)
|
|
|
|
|
{
|
2010-05-20 14:19:57 -07:00
|
|
|
printf (" ");
|
2010-05-20 14:29:43 -07:00
|
|
|
}
|
2010-05-20 14:19:57 -07:00
|
|
|
printf ("%s", $2);
|
2010-05-20 14:29:43 -07:00
|
|
|
|
2010-05-20 14:19:57 -07:00
|
|
|
if (is_not_separator)
|
2010-05-20 14:29:43 -07:00
|
|
|
parser->just_printed_separator = 0;
|
2010-05-20 14:19:57 -07:00
|
|
|
else
|
2010-05-20 14:29:43 -07:00
|
|
|
parser->just_printed_separator = 1;
|
2010-05-20 14:19:57 -07:00
|
|
|
}
|
2010-05-20 14:29:43 -07:00
|
|
|
|
2010-05-20 14:19:57 -07:00
|
|
|
if ($2)
|
|
|
|
|
talloc_free ($2);
|
2010-05-20 14:38:06 -07:00
|
|
|
|
|
|
|
|
if (parser->need_newline) {
|
|
|
|
|
printf ("\n");
|
|
|
|
|
parser->just_printed_separator = 1;
|
|
|
|
|
parser->need_newline = 0;
|
|
|
|
|
}
|
2010-05-20 14:19:57 -07:00
|
|
|
}
|
2010-05-10 11:44:09 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
content:
|
2010-05-13 07:38:29 -07:00
|
|
|
IDENTIFIER {
|
2010-05-20 14:19:57 -07:00
|
|
|
$$ = $1;
|
2010-05-13 07:38:29 -07:00
|
|
|
}
|
2010-05-20 08:01:44 -07:00
|
|
|
| IDENTIFIER_FINALIZED {
|
2010-05-20 14:19:57 -07:00
|
|
|
$$ = $1;
|
2010-05-13 07:38:29 -07:00
|
|
|
}
|
2010-05-20 08:01:44 -07:00
|
|
|
| TOKEN {
|
2010-05-20 14:19:57 -07:00
|
|
|
$$ = $1.value;
|
2010-05-20 08:01:44 -07:00
|
|
|
}
|
2010-05-17 10:34:29 -07:00
|
|
|
| FUNC_MACRO {
|
2010-05-20 14:19:57 -07:00
|
|
|
$$ = $1;
|
2010-05-17 10:34:29 -07:00
|
|
|
}
|
2010-05-18 22:10:04 -07:00
|
|
|
| directive {
|
2010-05-20 14:19:57 -07:00
|
|
|
$$ = talloc_strdup (parser, "\n");
|
|
|
|
|
}
|
|
|
|
|
| punctuator {
|
|
|
|
|
$$ = talloc_asprintf (parser, "%c", $1);
|
|
|
|
|
}
|
|
|
|
|
| macro {
|
|
|
|
|
$$ = NULL;
|
2010-05-14 10:31:43 -07:00
|
|
|
}
|
2010-05-12 13:11:50 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-20 14:19:57 -07:00
|
|
|
punctuator:
|
|
|
|
|
'(' { $$ = '('; }
|
|
|
|
|
| ')' { $$ = ')'; }
|
|
|
|
|
| ',' { $$ = ','; }
|
|
|
|
|
;
|
|
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
macro:
|
|
|
|
|
FUNC_MACRO '(' argument_list ')' {
|
2010-05-18 22:10:04 -07:00
|
|
|
_expand_function_macro (parser, $1, $3);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
|
|
|
|
| OBJ_MACRO {
|
2010-05-18 22:10:04 -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-19 07:49:47 -07:00
|
|
|
argument_word {
|
2010-05-19 13:54:37 -07:00
|
|
|
$$ = _token_list_create (parser);
|
2010-05-20 08:01:44 -07:00
|
|
|
_token_list_append ($$, $1.type, $1.value);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
2010-05-19 07:49:47 -07:00
|
|
|
| argument argument_word {
|
2010-05-20 08:01:44 -07:00
|
|
|
_token_list_append ($1, $2.type, $2.value);
|
|
|
|
|
talloc_free ($2.value);
|
2010-05-14 16:53:52 -07:00
|
|
|
$$ = $1;
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
2010-05-20 08:42:02 -07:00
|
|
|
| argument '(' argument_or_comma ')' {
|
2010-05-19 13:54:37 -07:00
|
|
|
_token_list_append ($1, '(', "(");
|
|
|
|
|
_token_list_append_list ($1, $3);
|
|
|
|
|
_token_list_append ($1, ')', ")");
|
2010-05-14 16:53:52 -07:00
|
|
|
$$ = $1;
|
2010-05-14 10:44:19 -07:00
|
|
|
}
|
2010-05-13 09:36:23 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-19 07:49:47 -07:00
|
|
|
argument_word:
|
2010-05-20 08:01:44 -07:00
|
|
|
IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
|
|
|
|
|
| IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
|
2010-05-19 07:49:47 -07:00
|
|
|
| TOKEN { $$ = $1; }
|
2010-05-20 08:01:44 -07:00
|
|
|
| FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
|
|
|
|
|
| macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
|
2010-05-19 07:49:47 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-20 08:42:02 -07:00
|
|
|
/* XXX: The body of argument_or_comma is the same as the body
|
|
|
|
|
* of argument, but with "argument" and "argument_word"
|
|
|
|
|
* changed to "argument_or_comma" and
|
|
|
|
|
* "argument_word_or_comma". It would be nice to have less
|
|
|
|
|
* redundancy here, but I'm not sure how.
|
|
|
|
|
*
|
|
|
|
|
* It would also be nice to have a less ugly grammar to have
|
|
|
|
|
* to implement, but such is the C preprocessor.
|
|
|
|
|
*/
|
|
|
|
|
argument_or_comma:
|
|
|
|
|
argument_word_or_comma {
|
|
|
|
|
$$ = _token_list_create (parser);
|
|
|
|
|
_token_list_append ($$, $1.type, $1.value);
|
|
|
|
|
}
|
|
|
|
|
| argument_or_comma argument_word_or_comma {
|
|
|
|
|
_token_list_append ($1, $2.type, $2.value);
|
|
|
|
|
$$ = $1;
|
|
|
|
|
}
|
|
|
|
|
| argument_or_comma '(' argument_or_comma ')' {
|
|
|
|
|
_token_list_append ($1, '(', "(");
|
|
|
|
|
_token_list_append_list ($1, $3);
|
|
|
|
|
_token_list_append ($1, ')', ")");
|
|
|
|
|
$$ = $1;
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
argument_word_or_comma:
|
|
|
|
|
IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
|
|
|
|
|
| IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
|
|
|
|
|
| TOKEN { $$ = $1; }
|
|
|
|
|
| FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
|
|
|
|
|
| macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
|
|
|
|
|
| ',' { $$.type = ','; $$.value = xtalloc_strdup (parser, ","); }
|
|
|
|
|
;
|
2010-05-19 07:49:47 -07:00
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
directive:
|
2010-05-19 13:28:24 -07:00
|
|
|
DEFINE IDENTIFIER NEWLINE {
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *list = _token_list_create (parser);
|
2010-05-19 13:28:24 -07:00
|
|
|
_define_object_macro (parser, $2, list);
|
2010-05-13 10:29:07 -07:00
|
|
|
}
|
2010-05-19 13:28:24 -07:00
|
|
|
| DEFINE IDENTIFIER SPACE replacement_list NEWLINE {
|
|
|
|
|
_define_object_macro (parser, $2, $4);
|
|
|
|
|
}
|
|
|
|
|
| DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE {
|
2010-05-14 17:08:45 -07:00
|
|
|
_define_function_macro (parser, $2, $4, $6);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
2010-05-20 22:27:07 -07:00
|
|
|
| IF expression NEWLINE {
|
|
|
|
|
_glcpp_parser_skip_stack_push_if (parser, $2);
|
|
|
|
|
}
|
|
|
|
|
| IFDEF IDENTIFIER NEWLINE {
|
|
|
|
|
string_list_t *macro = hash_table_find (parser->defines, $2);
|
|
|
|
|
talloc_free ($2);
|
|
|
|
|
_glcpp_parser_skip_stack_push_if (parser, macro != NULL);
|
|
|
|
|
}
|
|
|
|
|
| IFNDEF IDENTIFIER NEWLINE {
|
|
|
|
|
string_list_t *macro = hash_table_find (parser->defines, $2);
|
|
|
|
|
talloc_free ($2);
|
|
|
|
|
_glcpp_parser_skip_stack_push_if (parser, macro == NULL);
|
|
|
|
|
}
|
|
|
|
|
| ELIF expression NEWLINE {
|
|
|
|
|
_glcpp_parser_skip_stack_change_if (parser, "#elif", $2);
|
|
|
|
|
}
|
|
|
|
|
| ELSE {
|
|
|
|
|
_glcpp_parser_skip_stack_change_if (parser, "else", 1);
|
|
|
|
|
}
|
|
|
|
|
| ENDIF {
|
|
|
|
|
_glcpp_parser_skip_stack_pop (parser);
|
|
|
|
|
}
|
2010-05-18 22:10:04 -07:00
|
|
|
| UNDEF IDENTIFIER {
|
|
|
|
|
string_list_t *macro = hash_table_find (parser->defines, $2);
|
|
|
|
|
if (macro) {
|
2010-05-12 13:11:50 -07:00
|
|
|
/* 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);
|
2010-05-18 22:10:04 -07:00
|
|
|
talloc_free (macro);
|
2010-05-12 13:11:50 -07:00
|
|
|
}
|
|
|
|
|
talloc_free ($2);
|
2010-05-12 12:17:10 -07:00
|
|
|
}
|
2010-05-10 11:44:09 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-20 22:27:07 -07:00
|
|
|
expression:
|
|
|
|
|
INTEGER {
|
|
|
|
|
$$ = $1;
|
|
|
|
|
}
|
2010-05-24 10:37:38 -07:00
|
|
|
| expression OR expression {
|
|
|
|
|
$$ = $1 || $3;
|
|
|
|
|
}
|
|
|
|
|
| expression AND expression {
|
|
|
|
|
$$ = $1 && $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '|' expression {
|
|
|
|
|
$$ = $1 | $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '^' expression {
|
|
|
|
|
$$ = $1 ^ $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '&' expression {
|
|
|
|
|
$$ = $1 & $3;
|
|
|
|
|
}
|
|
|
|
|
| expression NOT_EQUAL expression {
|
|
|
|
|
$$ = $1 != $3;
|
|
|
|
|
}
|
|
|
|
|
| expression EQUAL expression {
|
|
|
|
|
$$ = $1 == $3;
|
|
|
|
|
}
|
|
|
|
|
| expression GREATER_OR_EQUAL expression {
|
|
|
|
|
$$ = $1 >= $3;
|
|
|
|
|
}
|
|
|
|
|
| expression LESS_OR_EQUAL expression {
|
|
|
|
|
$$ = $1 <= $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '>' expression {
|
|
|
|
|
$$ = $1 > $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '<' expression {
|
|
|
|
|
$$ = $1 < $3;
|
|
|
|
|
}
|
|
|
|
|
| expression RIGHT_SHIFT expression {
|
|
|
|
|
$$ = $1 >> $3;
|
|
|
|
|
}
|
|
|
|
|
| expression LEFT_SHIFT expression {
|
|
|
|
|
$$ = $1 << $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '-' expression {
|
|
|
|
|
$$ = $1 - $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '+' expression {
|
|
|
|
|
$$ = $1 + $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '%' expression {
|
|
|
|
|
$$ = $1 % $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '/' expression {
|
|
|
|
|
$$ = $1 / $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '*' expression {
|
|
|
|
|
$$ = $1 * $3;
|
|
|
|
|
}
|
|
|
|
|
| '!' expression %prec UNARY {
|
|
|
|
|
$$ = ! $2;
|
|
|
|
|
}
|
|
|
|
|
| '~' expression %prec UNARY {
|
|
|
|
|
$$ = ~ $2;
|
|
|
|
|
}
|
|
|
|
|
| '-' expression %prec UNARY {
|
|
|
|
|
$$ = - $2;
|
|
|
|
|
}
|
|
|
|
|
| '+' expression %prec UNARY {
|
|
|
|
|
$$ = + $2;
|
|
|
|
|
}
|
|
|
|
|
| DEFINED IDENTIFIER %prec UNARY {
|
|
|
|
|
string_list_t *macro = hash_table_find (parser->defines, $2);
|
|
|
|
|
talloc_free ($2);
|
|
|
|
|
if (macro)
|
|
|
|
|
$$ = 1;
|
|
|
|
|
else
|
|
|
|
|
$$ = 0;
|
|
|
|
|
}
|
|
|
|
|
| '(' expression ')' {
|
|
|
|
|
$$ = $2;
|
|
|
|
|
}
|
2010-05-20 22:27:07 -07:00
|
|
|
;
|
|
|
|
|
|
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-18 22:10:04 -07:00
|
|
|
| IDENTIFIER {
|
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-18 22:10:04 -07:00
|
|
|
| parameter_list ',' IDENTIFIER {
|
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-19 13:28:24 -07:00
|
|
|
replacement_list:
|
|
|
|
|
/* empty */ {
|
2010-05-19 13:54:37 -07:00
|
|
|
$$ = _token_list_create (parser);
|
2010-05-19 13:28:24 -07:00
|
|
|
}
|
|
|
|
|
| pp_tokens {
|
|
|
|
|
$$ = $1;
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pp_tokens:
|
|
|
|
|
TOKEN {
|
2010-05-19 13:54:37 -07:00
|
|
|
$$ = _token_list_create (parser);
|
2010-05-20 08:01:44 -07:00
|
|
|
_token_list_append ($$, $1.type, $1.value);
|
2010-05-19 13:28:24 -07:00
|
|
|
}
|
|
|
|
|
| pp_tokens TOKEN {
|
2010-05-20 08:01:44 -07:00
|
|
|
_token_list_append ($1, $2.type, $2.value);
|
2010-05-19 13:28:24 -07:00
|
|
|
$$ = $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) {
|
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;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
token_list_t *
|
|
|
|
|
_token_list_create (void *ctx)
|
|
|
|
|
{
|
|
|
|
|
token_list_t *list;
|
|
|
|
|
|
|
|
|
|
list = xtalloc (ctx, token_list_t);
|
|
|
|
|
list->head = NULL;
|
|
|
|
|
list->tail = NULL;
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_token_list_append (token_list_t *list, int type, const char *value)
|
|
|
|
|
{
|
|
|
|
|
token_node_t *node;
|
|
|
|
|
|
|
|
|
|
node = xtalloc (list, token_node_t);
|
|
|
|
|
node->type = type;
|
|
|
|
|
node->value = xtalloc_strdup (list, value);
|
|
|
|
|
|
|
|
|
|
node->next = NULL;
|
|
|
|
|
|
|
|
|
|
if (list->head == NULL) {
|
|
|
|
|
list->head = node;
|
|
|
|
|
} else {
|
|
|
|
|
list->tail->next = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->tail = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-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-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-18 22:10:04 -07:00
|
|
|
parser->expansions = NULL;
|
|
|
|
|
|
2010-05-20 14:29:43 -07:00
|
|
|
parser->just_printed_separator = 1;
|
2010-05-20 14:38:06 -07:00
|
|
|
parser->need_newline = 0;
|
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 14:38:06 -07:00
|
|
|
if (parser->need_newline)
|
|
|
|
|
printf ("\n");
|
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-19 07:29:22 -07:00
|
|
|
static int
|
|
|
|
|
glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
|
|
|
|
|
{
|
|
|
|
|
expansion_node_t *node;
|
|
|
|
|
|
|
|
|
|
for (node = parser->expansions; node; node = node->next) {
|
|
|
|
|
if (node->macro &&
|
|
|
|
|
strcmp (node->macro->identifier, member) == 0)
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
token_class_t
|
|
|
|
|
glcpp_parser_classify_token (glcpp_parser_t *parser,
|
|
|
|
|
const char *identifier,
|
|
|
|
|
int *parameter_index)
|
2010-05-13 07:38:29 -07:00
|
|
|
{
|
2010-05-13 09:36:23 -07:00
|
|
|
macro_t *macro;
|
|
|
|
|
|
2010-05-20 15:15:26 -07:00
|
|
|
/* Is this token a defined macro? */
|
2010-05-13 09:36:23 -07:00
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
|
|
|
|
|
if (macro == NULL)
|
2010-05-18 22:10:04 -07:00
|
|
|
return TOKEN_CLASS_IDENTIFIER;
|
2010-05-13 09:36:23 -07:00
|
|
|
|
2010-05-19 07:29:22 -07:00
|
|
|
/* Don't consider this a macro if we are already actively
|
|
|
|
|
* expanding this macro. */
|
|
|
|
|
if (glcpp_parser_is_expanding (parser, identifier))
|
2010-05-20 08:01:44 -07:00
|
|
|
return TOKEN_CLASS_IDENTIFIER_FINALIZED;
|
2010-05-19 07:29:22 -07:00
|
|
|
|
|
|
|
|
/* Definitely a macro. Just need to check if it's function-like. */
|
2010-05-13 09:36:23 -07:00
|
|
|
if (macro->is_function)
|
2010-05-18 22:10:04 -07:00
|
|
|
return TOKEN_CLASS_FUNC_MACRO;
|
2010-05-13 09:36:23 -07:00
|
|
|
else
|
2010-05-18 22:10:04 -07:00
|
|
|
return TOKEN_CLASS_OBJ_MACRO;
|
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-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-18 22:10:04 -07:00
|
|
|
static void
|
2010-05-20 15:15:26 -07:00
|
|
|
_glcpp_parser_push_expansion (glcpp_parser_t *parser,
|
|
|
|
|
macro_t *macro,
|
|
|
|
|
token_node_t *replacements)
|
2010-05-18 22:10:04 -07:00
|
|
|
{
|
|
|
|
|
expansion_node_t *node;
|
|
|
|
|
|
|
|
|
|
node = xtalloc (parser, expansion_node_t);
|
|
|
|
|
|
|
|
|
|
node->macro = macro;
|
2010-05-19 13:28:24 -07:00
|
|
|
node->replacements = replacements;
|
2010-05-18 22:10:04 -07:00
|
|
|
|
|
|
|
|
node->next = parser->expansions;
|
|
|
|
|
parser->expansions = node;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-19 13:28:24 -07:00
|
|
|
static void
|
2010-05-18 22:10:04 -07:00
|
|
|
glcpp_parser_pop_expansion (glcpp_parser_t *parser)
|
2010-05-13 12:56:42 -07:00
|
|
|
{
|
2010-05-18 22:10:04 -07:00
|
|
|
expansion_node_t *node;
|
|
|
|
|
|
|
|
|
|
node = parser->expansions;
|
2010-05-17 10:15:23 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
if (node == NULL) {
|
|
|
|
|
fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
2010-05-17 10:15:23 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
parser->expansions = node->next;
|
2010-05-13 12:56:42 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
talloc_free (node);
|
|
|
|
|
}
|
2010-05-14 10:31:43 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
void
|
2010-05-14 10:31:43 -07:00
|
|
|
_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-19 07:29:22 -07:00
|
|
|
assert (! glcpp_parser_is_expanding (parser, identifier));
|
2010-05-13 09:36:23 -07:00
|
|
|
|
2010-05-20 15:15:26 -07:00
|
|
|
_glcpp_parser_push_expansion (parser, macro, macro->replacements->head);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
void
|
2010-05-14 10:31:43 -07:00
|
|
|
_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
|
|
|
{
|
|
|
|
|
macro_t *macro;
|
2010-05-20 15:15:26 -07:00
|
|
|
token_list_t *expanded;
|
|
|
|
|
token_node_t *i, *j;
|
|
|
|
|
int parameter_index;
|
2010-05-13 09:36:23 -07:00
|
|
|
|
|
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
assert (macro->is_function);
|
2010-05-19 07:29:22 -07:00
|
|
|
assert (! glcpp_parser_is_expanding (parser, identifier));
|
2010-05-13 09:36:23 -07:00
|
|
|
|
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-18 22:10:04 -07:00
|
|
|
return;
|
2010-05-13 12:56:42 -07:00
|
|
|
}
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-20 15:15:26 -07:00
|
|
|
expanded = _token_list_create (macro);
|
|
|
|
|
|
|
|
|
|
for (i = macro->replacements->head; i; i = i->next) {
|
|
|
|
|
if (_string_list_contains (macro->parameters, i->value,
|
|
|
|
|
¶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->type,
|
|
|
|
|
j->value);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
_token_list_append (expanded, i->type, i->value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_glcpp_parser_push_expansion (parser, macro, expanded->head);
|
2010-05-12 12:17:10 -07:00
|
|
|
}
|
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-19 13:28:24 -07:00
|
|
|
expansion_node_t *expansion;
|
2010-05-19 13:54:37 -07:00
|
|
|
token_node_t *replacements;
|
2010-05-19 13:28:24 -07:00
|
|
|
int parameter_index;
|
2010-05-20 15:18:54 -07:00
|
|
|
const char *token;
|
|
|
|
|
token_class_t class;
|
2010-05-19 13:28:24 -07:00
|
|
|
|
|
|
|
|
/* Who says C can't do efficient tail recursion? */
|
|
|
|
|
RECURSE:
|
|
|
|
|
|
|
|
|
|
expansion = parser->expansions;
|
|
|
|
|
|
|
|
|
|
if (expansion == NULL)
|
|
|
|
|
return glcpp_lex (parser->scanner);
|
|
|
|
|
|
|
|
|
|
replacements = expansion->replacements;
|
|
|
|
|
|
|
|
|
|
/* Pop expansion when replacements is exhausted. */
|
|
|
|
|
if (replacements == NULL) {
|
|
|
|
|
glcpp_parser_pop_expansion (parser);
|
|
|
|
|
goto RECURSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expansion->replacements = replacements->next;
|
|
|
|
|
|
2010-05-20 15:18:54 -07:00
|
|
|
token = replacements->value;
|
|
|
|
|
|
|
|
|
|
/* Implement token pasting. */
|
|
|
|
|
if (replacements->next && strcmp (replacements->next->value, "##") == 0) {
|
|
|
|
|
token_node_t *next_node;
|
|
|
|
|
|
|
|
|
|
next_node = replacements->next->next;
|
|
|
|
|
|
|
|
|
|
if (next_node == NULL) {
|
|
|
|
|
fprintf (stderr, "Error: '##' cannot appear at the end of a macro expansion.\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token = xtalloc_asprintf (parser, "%s%s",
|
|
|
|
|
token, next_node->value);
|
|
|
|
|
expansion->replacements = next_node->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (strcmp (token, "(") == 0)
|
2010-05-19 13:28:24 -07:00
|
|
|
return '(';
|
2010-05-20 15:18:54 -07:00
|
|
|
else if (strcmp (token, ")") == 0)
|
2010-05-19 13:28:24 -07:00
|
|
|
return ')';
|
|
|
|
|
|
2010-05-20 15:18:54 -07:00
|
|
|
yylval.str = xtalloc_strdup (parser, token);
|
2010-05-19 13:28:24 -07:00
|
|
|
|
2010-05-20 08:01:44 -07:00
|
|
|
/* Carefully refuse to expand any finalized identifier. */
|
|
|
|
|
if (replacements->type == IDENTIFIER_FINALIZED)
|
|
|
|
|
return IDENTIFIER_FINALIZED;
|
|
|
|
|
|
2010-05-19 13:28:24 -07:00
|
|
|
switch (glcpp_parser_classify_token (parser, yylval.str,
|
|
|
|
|
¶meter_index))
|
|
|
|
|
{
|
|
|
|
|
case TOKEN_CLASS_IDENTIFIER:
|
|
|
|
|
return IDENTIFIER;
|
|
|
|
|
break;
|
2010-05-20 08:01:44 -07:00
|
|
|
case TOKEN_CLASS_IDENTIFIER_FINALIZED:
|
|
|
|
|
return IDENTIFIER_FINALIZED;
|
|
|
|
|
break;
|
2010-05-19 13:28:24 -07:00
|
|
|
case TOKEN_CLASS_FUNC_MACRO:
|
|
|
|
|
return FUNC_MACRO;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
case TOKEN_CLASS_OBJ_MACRO:
|
|
|
|
|
return OBJ_MACRO;
|
|
|
|
|
break;
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|