2020-12-07 10:56:30 -08:00
|
|
|
/*
|
|
|
|
* HLSL preprocessor
|
|
|
|
*
|
|
|
|
* Copyright 2020 Zebediah Figura for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
%{
|
|
|
|
|
|
|
|
#include "preproc.tab.h"
|
|
|
|
|
2022-02-08 06:09:51 -08:00
|
|
|
#undef ERROR /* defined in wingdi.h */
|
|
|
|
|
2020-12-07 10:56:30 -08:00
|
|
|
#define YYSTYPE PREPROC_YYSTYPE
|
|
|
|
#define YYLTYPE PREPROC_YYLTYPE
|
|
|
|
|
|
|
|
#define YY_DECL static int preproc_lexer_lex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param, yyscan_t yyscanner)
|
|
|
|
|
2023-07-29 16:08:29 -07:00
|
|
|
static struct preproc_macro *preproc_get_top_macro(struct preproc_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (!ctx->expansion_count)
|
|
|
|
return NULL;
|
|
|
|
return ctx->expansion_stack[ctx->expansion_count - 1].macro;
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:13:20 -08:00
|
|
|
static void update_location(struct preproc_ctx *ctx);
|
|
|
|
|
|
|
|
#define YY_USER_ACTION update_location(yyget_extra(yyscanner));
|
|
|
|
|
2020-12-07 10:56:30 -08:00
|
|
|
%}
|
|
|
|
|
|
|
|
%option 8bit
|
|
|
|
%option bison-bridge
|
|
|
|
%option bison-locations
|
|
|
|
%option extra-type="struct preproc_ctx *"
|
|
|
|
%option never-interactive
|
2023-05-08 10:57:22 -07:00
|
|
|
%option nodefault
|
2020-12-07 10:56:30 -08:00
|
|
|
%option noinput
|
|
|
|
%option nounput
|
2020-12-07 10:56:31 -08:00
|
|
|
%option noyy_top_state
|
2020-12-07 10:56:30 -08:00
|
|
|
%option noyywrap
|
|
|
|
%option prefix="preproc_yy"
|
|
|
|
%option reentrant
|
2020-12-07 10:56:31 -08:00
|
|
|
%option stack
|
|
|
|
|
|
|
|
/* Because these can both be terminated by EOF, we need states for them. */
|
|
|
|
%s C_COMMENT
|
|
|
|
%s CXX_COMMENT
|
2020-12-07 10:56:30 -08:00
|
|
|
|
2021-01-14 12:47:50 -08:00
|
|
|
%s ERROR
|
2021-01-07 09:48:09 -08:00
|
|
|
%s INCLUDE
|
2021-01-25 09:23:56 -08:00
|
|
|
%s LINE
|
2021-01-07 09:48:09 -08:00
|
|
|
|
2020-12-21 12:37:06 -08:00
|
|
|
NEWLINE \r?\n
|
2024-05-22 10:07:00 -07:00
|
|
|
WS [ \t\r]
|
2024-09-20 08:35:38 -07:00
|
|
|
IDENTIFIER (::)?[A-Za-z_]((::)?[A-Za-z0-9_]+)*
|
2022-02-02 04:46:16 -08:00
|
|
|
INT_SUFFIX [uUlL]{0,2}
|
2020-12-07 10:56:30 -08:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
2020-12-07 10:56:31 -08:00
|
|
|
<INITIAL>"//" {yy_push_state(CXX_COMMENT, yyscanner);}
|
|
|
|
<INITIAL>"/*" {yy_push_state(C_COMMENT, yyscanner);}
|
2020-12-21 12:37:06 -08:00
|
|
|
<CXX_COMMENT>\\{NEWLINE} {}
|
2020-12-07 10:56:31 -08:00
|
|
|
<CXX_COMMENT>\n {
|
|
|
|
yy_pop_state(yyscanner);
|
2021-01-07 09:48:09 -08:00
|
|
|
BEGIN(INITIAL);
|
2020-12-21 12:37:06 -08:00
|
|
|
return T_NEWLINE;
|
2020-12-07 10:56:31 -08:00
|
|
|
}
|
|
|
|
<C_COMMENT>"*/" {yy_pop_state(yyscanner);}
|
|
|
|
<C_COMMENT,CXX_COMMENT><<EOF>> {yy_pop_state(yyscanner);}
|
|
|
|
<C_COMMENT,CXX_COMMENT>. {}
|
2023-05-08 11:06:31 -07:00
|
|
|
<C_COMMENT>\n {}
|
2020-12-07 10:56:31 -08:00
|
|
|
|
2021-01-14 12:47:50 -08:00
|
|
|
<ERROR>(\\{NEWLINE}|[^\n])* {return T_STRING;}
|
|
|
|
|
2021-01-14 12:47:51 -08:00
|
|
|
<INITIAL>defined/\( {return T_DEFINED;}
|
|
|
|
<INITIAL>defined {return T_DEFINED;}
|
2021-01-12 14:14:18 -08:00
|
|
|
<INITIAL>{IDENTIFIER}/\( {return T_IDENTIFIER_PAREN;}
|
2021-01-07 09:48:06 -08:00
|
|
|
<INITIAL>{IDENTIFIER} {return T_IDENTIFIER;}
|
2020-12-07 10:56:32 -08:00
|
|
|
|
2021-01-19 09:18:21 -08:00
|
|
|
<INITIAL>"<=" {return T_LE;}
|
|
|
|
<INITIAL>">=" {return T_GE;}
|
2021-01-21 14:10:43 -08:00
|
|
|
<INITIAL>"==" {return T_EQ;}
|
|
|
|
<INITIAL>"!=" {return T_NE;}
|
2021-01-21 14:10:45 -08:00
|
|
|
<INITIAL>"&&" {return T_AND;}
|
|
|
|
<INITIAL>"||" {return T_OR;}
|
2021-01-19 09:18:21 -08:00
|
|
|
|
2020-12-21 12:37:06 -08:00
|
|
|
/* We have no use for floats, but shouldn't parse them as integers. */
|
|
|
|
|
2020-12-07 10:56:32 -08:00
|
|
|
<INITIAL>[0-9]*\.[0-9]+([eE][+-]?[0-9]+)?[hHfF]? {return T_TEXT;}
|
|
|
|
<INITIAL>[0-9]+\.([eE][+-]?[0-9]+)?[hHfF]? {return T_TEXT;}
|
|
|
|
<INITIAL>[0-9]+([eE][+-]?[0-9]+)?[hHfF] {return T_TEXT;}
|
|
|
|
<INITIAL>[0-9]+[eE][+-]?[0-9]+ {return T_TEXT;}
|
2022-02-02 04:46:16 -08:00
|
|
|
<INITIAL,LINE>0[xX][0-9a-fA-f]+{INT_SUFFIX} {return T_INTEGER;}
|
|
|
|
<INITIAL,LINE>0[0-7]*{INT_SUFFIX} {return T_INTEGER;}
|
|
|
|
<INITIAL,LINE>[1-9][0-9]*{INT_SUFFIX} {return T_INTEGER;}
|
2020-12-07 10:56:32 -08:00
|
|
|
|
2021-01-12 14:14:20 -08:00
|
|
|
<INITIAL>## {return T_CONCAT;}
|
|
|
|
|
2020-12-07 10:56:32 -08:00
|
|
|
<INITIAL>"++" {return T_TEXT;}
|
|
|
|
<INITIAL>"--" {return T_TEXT;}
|
|
|
|
<INITIAL>"<<"=? {return T_TEXT;}
|
|
|
|
<INITIAL>">>"=? {return T_TEXT;}
|
2021-01-21 14:10:43 -08:00
|
|
|
<INITIAL>[-+*/%&|^]= {return T_TEXT;}
|
2020-12-07 10:56:32 -08:00
|
|
|
|
2021-01-25 09:23:58 -08:00
|
|
|
/* Native doesn't preserve these tokens when running the preprocessor on its
|
|
|
|
* own, but there's no good reason to emulate that difference yet. */
|
|
|
|
<INITIAL>[pv]s\.[123]\.[0-4x] {return T_TEXT;}
|
|
|
|
|
2021-01-25 09:23:56 -08:00
|
|
|
<INCLUDE,LINE>\"[^"]*\" {return T_STRING;}
|
2021-01-07 09:48:09 -08:00
|
|
|
<INCLUDE>\<[^>]*\> {return T_STRING;}
|
|
|
|
|
2020-12-07 10:56:32 -08:00
|
|
|
/* C strings (including escaped quotes). */
|
|
|
|
<INITIAL>\"([^"\\]|\\.)*\" {return T_TEXT;}
|
|
|
|
|
2020-12-21 12:37:06 -08:00
|
|
|
<INITIAL>#{WS}*{IDENTIFIER} {
|
|
|
|
struct preproc_ctx *ctx = yyget_extra(yyscanner);
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (!ctx->last_was_newline)
|
2023-07-29 16:08:29 -07:00
|
|
|
{
|
|
|
|
struct preproc_macro *macro;
|
|
|
|
|
|
|
|
/* Stringification is only done for function-like macro bodies.
|
|
|
|
* Anywhere else, we need to parse it as two separate tokens.
|
|
|
|
* We could use a state for this, but yyless() is easier and cheap.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((macro = preproc_get_top_macro(ctx)) && macro->arg_count)
|
|
|
|
return T_HASHSTRING;
|
|
|
|
|
|
|
|
yyless(1);
|
|
|
|
return T_TEXT;
|
|
|
|
}
|
2020-12-21 12:37:06 -08:00
|
|
|
|
|
|
|
for (p = yytext + 1; strchr(" \t", *p); ++p)
|
|
|
|
;
|
|
|
|
|
2021-01-14 12:47:50 -08:00
|
|
|
if (!strcmp(p, "error"))
|
|
|
|
{
|
|
|
|
BEGIN(ERROR);
|
|
|
|
return T_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-01-07 09:48:09 -08:00
|
|
|
if (!strcmp(p, "include"))
|
|
|
|
{
|
|
|
|
BEGIN(INCLUDE);
|
|
|
|
return T_INCLUDE;
|
|
|
|
}
|
|
|
|
|
2021-01-25 09:23:56 -08:00
|
|
|
if (!strcmp(p, "line"))
|
|
|
|
{
|
|
|
|
BEGIN(LINE);
|
|
|
|
return T_LINE;
|
|
|
|
}
|
|
|
|
|
2021-01-07 09:48:06 -08:00
|
|
|
if (!strcmp(p, "define"))
|
|
|
|
return T_DEFINE;
|
2020-12-15 15:13:23 -08:00
|
|
|
if (!strcmp(p, "elif"))
|
|
|
|
return T_ELIF;
|
2020-12-15 15:13:22 -08:00
|
|
|
if (!strcmp(p, "else"))
|
|
|
|
return T_ELSE;
|
2020-12-21 12:37:06 -08:00
|
|
|
if (!strcmp(p, "endif"))
|
|
|
|
return T_ENDIF;
|
|
|
|
if (!strcmp(p, "if"))
|
|
|
|
return T_IF;
|
2021-01-07 09:48:07 -08:00
|
|
|
if (!strcmp(p, "ifdef"))
|
|
|
|
return T_IFDEF;
|
2021-01-07 09:48:08 -08:00
|
|
|
if (!strcmp(p, "ifndef"))
|
|
|
|
return T_IFNDEF;
|
2021-01-14 12:47:49 -08:00
|
|
|
if (!strcmp(p, "pragma"))
|
|
|
|
return T_PRAGMA;
|
2021-01-07 09:48:10 -08:00
|
|
|
if (!strcmp(p, "undef"))
|
|
|
|
return T_UNDEF;
|
2020-12-21 12:37:06 -08:00
|
|
|
|
|
|
|
preproc_warning(ctx, yyget_lloc(yyscanner), VKD3D_SHADER_WARNING_PP_UNKNOWN_DIRECTIVE,
|
|
|
|
"Ignoring unknown directive \"%s\".", yytext);
|
|
|
|
return T_TEXT;
|
|
|
|
}
|
|
|
|
|
2021-01-25 09:23:56 -08:00
|
|
|
<INITIAL,INCLUDE,LINE>\\{NEWLINE} {}
|
|
|
|
<INITIAL,INCLUDE,ERROR,LINE>{NEWLINE} {
|
2021-01-07 09:48:09 -08:00
|
|
|
BEGIN(INITIAL);
|
|
|
|
return T_NEWLINE;
|
|
|
|
}
|
2020-12-21 12:37:06 -08:00
|
|
|
|
2023-05-08 10:52:10 -07:00
|
|
|
<INITIAL,INCLUDE,LINE>{WS}+ {}
|
2021-01-21 14:10:46 -08:00
|
|
|
<INITIAL>[-()\[\]{},+!*/<>&|^?:] {return yytext[0];}
|
2023-05-08 10:52:10 -07:00
|
|
|
<INITIAL,INCLUDE,LINE>. {return T_TEXT;}
|
2020-12-07 10:56:30 -08:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
2020-12-15 15:13:20 -08:00
|
|
|
static void update_location(struct preproc_ctx *ctx)
|
|
|
|
{
|
2021-01-07 09:48:09 -08:00
|
|
|
struct preproc_buffer *buffer = &preproc_get_top_file(ctx)->buffer;
|
2020-12-15 15:13:20 -08:00
|
|
|
unsigned int i, leng = yyget_leng(ctx->scanner);
|
|
|
|
const char *text = yyget_text(ctx->scanner);
|
|
|
|
|
|
|
|
/* We want to do this here, rather than before calling yylex(), because
|
|
|
|
* some tokens are skipped by the lexer. */
|
|
|
|
|
2021-01-07 09:48:09 -08:00
|
|
|
*yyget_lloc(ctx->scanner) = buffer->location;
|
2020-12-15 15:13:20 -08:00
|
|
|
|
|
|
|
for (i = 0; i < leng; ++i)
|
|
|
|
{
|
2021-01-07 09:48:09 -08:00
|
|
|
++buffer->location.column;
|
2020-12-15 15:13:20 -08:00
|
|
|
if (text[i] == '\n')
|
|
|
|
{
|
2021-01-07 09:48:09 -08:00
|
|
|
buffer->location.column = 1;
|
|
|
|
++buffer->location.line;
|
2020-12-15 15:13:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:14:19 -08:00
|
|
|
static bool preproc_is_writing(struct preproc_ctx *ctx)
|
|
|
|
{
|
|
|
|
const struct preproc_file *file;
|
|
|
|
|
|
|
|
/* This can happen while checking for unterminated macro invocation. */
|
|
|
|
if (!ctx->file_count)
|
|
|
|
return true;
|
|
|
|
file = preproc_get_top_file(ctx);
|
|
|
|
if (!file->if_count)
|
|
|
|
return true;
|
|
|
|
return file->if_stack[file->if_count - 1].current_true;
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:14:20 -08:00
|
|
|
/* Concatenation is not done for object-like macros, but is done for both
|
|
|
|
* function-like macro bodies and their arguments. */
|
|
|
|
static bool should_concat(struct preproc_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct preproc_macro *macro;
|
|
|
|
|
|
|
|
if (!ctx->expansion_count)
|
|
|
|
return false;
|
|
|
|
macro = ctx->expansion_stack[ctx->expansion_count - 1].macro;
|
|
|
|
return !macro || macro->arg_count;
|
|
|
|
}
|
|
|
|
|
2021-01-07 09:48:09 -08:00
|
|
|
static void preproc_pop_buffer(struct preproc_ctx *ctx)
|
|
|
|
{
|
2021-01-12 14:14:17 -08:00
|
|
|
if (ctx->expansion_count)
|
|
|
|
{
|
|
|
|
struct preproc_expansion *exp = &ctx->expansion_stack[ctx->expansion_count - 1];
|
2021-01-07 09:48:09 -08:00
|
|
|
|
2021-01-12 14:14:17 -08:00
|
|
|
yy_delete_buffer(exp->buffer.lexer_buffer, ctx->scanner);
|
2021-01-07 09:48:09 -08:00
|
|
|
|
2021-01-12 14:14:17 -08:00
|
|
|
--ctx->expansion_count;
|
|
|
|
TRACE("Expansion stack size is now %zu.\n", ctx->expansion_count);
|
|
|
|
}
|
|
|
|
else
|
2021-01-07 09:48:09 -08:00
|
|
|
{
|
2021-01-12 14:14:17 -08:00
|
|
|
struct preproc_file *file = preproc_get_top_file(ctx);
|
2021-01-07 09:48:09 -08:00
|
|
|
|
2021-01-12 14:14:17 -08:00
|
|
|
if (ctx->file_count > 1)
|
|
|
|
preproc_close_include(ctx, &file->code);
|
|
|
|
|
|
|
|
if (file->if_count)
|
|
|
|
{
|
|
|
|
const struct vkd3d_shader_location loc = {.source_name = file->filename};
|
2021-01-07 09:48:09 -08:00
|
|
|
|
2021-01-12 14:14:17 -08:00
|
|
|
preproc_warning(ctx, &loc, VKD3D_SHADER_WARNING_PP_UNTERMINATED_IF, "Unterminated #if block.");
|
|
|
|
}
|
|
|
|
vkd3d_free(file->if_stack);
|
2021-01-07 09:48:09 -08:00
|
|
|
|
2021-01-12 14:14:17 -08:00
|
|
|
vkd3d_free(file->filename);
|
2021-01-07 09:48:09 -08:00
|
|
|
|
2021-01-12 14:14:17 -08:00
|
|
|
yy_delete_buffer(file->buffer.lexer_buffer, ctx->scanner);
|
2021-01-07 09:48:09 -08:00
|
|
|
|
2021-01-12 14:14:17 -08:00
|
|
|
--ctx->file_count;
|
|
|
|
TRACE("File stack size is now %zu.\n", ctx->file_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->expansion_count)
|
|
|
|
yy_switch_to_buffer(ctx->expansion_stack[ctx->expansion_count - 1].buffer.lexer_buffer, ctx->scanner);
|
|
|
|
else if (ctx->file_count)
|
2021-01-07 09:48:09 -08:00
|
|
|
yy_switch_to_buffer(ctx->file_stack[ctx->file_count - 1].buffer.lexer_buffer, ctx->scanner);
|
|
|
|
}
|
|
|
|
|
2020-12-21 12:37:06 -08:00
|
|
|
static int return_token(int token, YYSTYPE *lval, const char *text)
|
|
|
|
{
|
|
|
|
switch (token)
|
|
|
|
{
|
2021-01-12 14:14:21 -08:00
|
|
|
case T_HASHSTRING:
|
2021-01-07 09:48:06 -08:00
|
|
|
case T_IDENTIFIER:
|
2021-01-12 14:14:18 -08:00
|
|
|
case T_IDENTIFIER_PAREN:
|
2020-12-21 12:37:06 -08:00
|
|
|
case T_INTEGER:
|
2021-01-07 09:48:09 -08:00
|
|
|
case T_STRING:
|
2020-12-21 12:37:06 -08:00
|
|
|
case T_TEXT:
|
|
|
|
if (!(lval->string = vkd3d_strdup(text)))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:14:19 -08:00
|
|
|
static const struct preproc_text *find_arg_expansion(struct preproc_ctx *ctx, const char *s)
|
|
|
|
{
|
|
|
|
struct preproc_macro *macro;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if ((macro = preproc_get_top_macro(ctx)))
|
|
|
|
{
|
|
|
|
for (i = 0; i < macro->arg_count; ++i)
|
|
|
|
{
|
|
|
|
if (!strcmp(s, macro->arg_names[i]))
|
|
|
|
return ¯o->arg_values[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void preproc_text_add(struct preproc_text *text, const char *string)
|
|
|
|
{
|
|
|
|
vkd3d_string_buffer_printf(&text->text, "%s", string);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool preproc_push_expansion(struct preproc_ctx *ctx,
|
|
|
|
const struct preproc_text *text, struct preproc_macro *macro)
|
2021-01-12 14:14:17 -08:00
|
|
|
{
|
|
|
|
struct preproc_expansion *exp;
|
|
|
|
|
|
|
|
if (!vkd3d_array_reserve((void **)&ctx->expansion_stack, &ctx->expansion_stack_size,
|
|
|
|
ctx->expansion_count + 1, sizeof(*ctx->expansion_stack)))
|
|
|
|
return false;
|
|
|
|
exp = &ctx->expansion_stack[ctx->expansion_count++];
|
|
|
|
exp->text = text;
|
|
|
|
exp->buffer.lexer_buffer = yy_scan_bytes(text->text.buffer, text->text.content_size, ctx->scanner);
|
|
|
|
exp->buffer.location = text->location;
|
2021-01-12 14:14:19 -08:00
|
|
|
exp->macro = macro;
|
2021-01-12 14:14:17 -08:00
|
|
|
TRACE("Expansion stack size is now %zu.\n", ctx->expansion_count);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-29 16:14:23 -07:00
|
|
|
static void preproc_stringify(struct preproc_ctx *ctx, struct vkd3d_string_buffer *buffer, const char *text)
|
|
|
|
{
|
|
|
|
const struct preproc_text *expansion;
|
|
|
|
const char *p = text + 1;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
++p;
|
|
|
|
|
|
|
|
vkd3d_string_buffer_printf(buffer, "\"");
|
|
|
|
if ((expansion = find_arg_expansion(ctx, p)))
|
|
|
|
{
|
2023-07-29 16:32:04 -07:00
|
|
|
size_t len = expansion->text.content_size;
|
|
|
|
size_t start = 0;
|
|
|
|
|
|
|
|
while (len && strchr(" \t\r\n", expansion->text.buffer[len - 1]))
|
|
|
|
--len;
|
|
|
|
|
|
|
|
while (start < len && strchr(" \t\r\n", expansion->text.buffer[start]))
|
|
|
|
++start;
|
|
|
|
|
|
|
|
for (i = start; i < len; ++i)
|
2023-07-29 16:14:23 -07:00
|
|
|
{
|
|
|
|
char c = expansion->text.buffer[i];
|
|
|
|
|
|
|
|
if (c == '\\' || c == '"')
|
|
|
|
vkd3d_string_buffer_printf(buffer, "\\");
|
|
|
|
vkd3d_string_buffer_printf(buffer, "%c", c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vkd3d_string_buffer_printf(buffer, "%s", p);
|
|
|
|
}
|
|
|
|
vkd3d_string_buffer_printf(buffer, "\"");
|
|
|
|
}
|
|
|
|
|
2020-12-07 10:56:30 -08:00
|
|
|
int yylex(YYSTYPE *lval, YYLTYPE *lloc, yyscan_t scanner)
|
|
|
|
{
|
|
|
|
struct preproc_ctx *ctx = yyget_extra(scanner);
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2021-01-12 14:14:19 -08:00
|
|
|
struct preproc_func_state *func_state;
|
2020-12-07 10:56:30 -08:00
|
|
|
const char *text;
|
|
|
|
int token;
|
|
|
|
|
2021-01-12 14:14:19 -08:00
|
|
|
if (ctx->lookahead_token)
|
2020-12-21 12:37:06 -08:00
|
|
|
{
|
2021-01-12 14:14:19 -08:00
|
|
|
token = ctx->lookahead_token;
|
|
|
|
text = yyget_text(scanner);
|
2020-12-21 12:37:06 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-12 14:14:19 -08:00
|
|
|
if (ctx->last_was_eof)
|
|
|
|
{
|
|
|
|
preproc_pop_buffer(ctx);
|
|
|
|
if (!ctx->file_count)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ctx->last_was_eof = false;
|
2020-12-21 12:37:06 -08:00
|
|
|
|
2024-08-02 01:34:34 -07:00
|
|
|
VKD3D_ASSERT(ctx->file_count);
|
2021-01-12 14:14:19 -08:00
|
|
|
if (!(token = preproc_lexer_lex(lval, lloc, scanner)))
|
|
|
|
{
|
|
|
|
ctx->last_was_eof = true;
|
|
|
|
|
|
|
|
/* If we have reached the end of an included file, inject a newline. */
|
|
|
|
if (ctx->expansion_count)
|
|
|
|
continue;
|
|
|
|
token = T_NEWLINE;
|
|
|
|
text = "\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
text = yyget_text(scanner);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->last_was_newline)
|
2020-12-21 12:37:06 -08:00
|
|
|
{
|
2021-01-12 14:14:19 -08:00
|
|
|
switch (token)
|
|
|
|
{
|
|
|
|
case T_DEFINE:
|
|
|
|
case T_ELIF:
|
|
|
|
case T_ELSE:
|
|
|
|
case T_ENDIF:
|
2021-01-14 12:47:50 -08:00
|
|
|
case T_ERROR:
|
2021-01-12 14:14:19 -08:00
|
|
|
case T_IF:
|
|
|
|
case T_IFDEF:
|
|
|
|
case T_IFNDEF:
|
|
|
|
case T_INCLUDE:
|
2021-01-25 09:23:56 -08:00
|
|
|
case T_LINE:
|
2021-01-14 12:47:49 -08:00
|
|
|
case T_PRAGMA:
|
2021-01-12 14:14:19 -08:00
|
|
|
case T_UNDEF:
|
|
|
|
ctx->current_directive = token;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ctx->current_directive = 0;
|
|
|
|
}
|
2020-12-21 12:37:06 -08:00
|
|
|
}
|
2021-01-12 14:14:19 -08:00
|
|
|
|
|
|
|
ctx->last_was_newline = (token == T_NEWLINE);
|
2020-12-21 12:37:06 -08:00
|
|
|
}
|
|
|
|
|
2021-01-14 12:47:51 -08:00
|
|
|
if (ctx->current_directive && token == T_DEFINED)
|
|
|
|
ctx->last_was_defined = true;
|
|
|
|
|
2021-01-12 14:14:19 -08:00
|
|
|
func_state = ctx->current_directive ? &ctx->directive_func : &ctx->text_func;
|
|
|
|
|
|
|
|
TRACE("Parsing token %d%s, line %d, in directive %d, state %#x, string %s.\n",
|
|
|
|
token, ctx->lookahead_token ? " (lookahead)" : "", lloc->line,
|
|
|
|
ctx->current_directive, func_state->state, debugstr_a(text));
|
2020-12-21 12:37:06 -08:00
|
|
|
|
2021-01-12 14:14:19 -08:00
|
|
|
ctx->lookahead_token = 0;
|
2020-12-21 12:37:06 -08:00
|
|
|
|
2021-01-07 09:48:06 -08:00
|
|
|
switch (ctx->current_directive)
|
|
|
|
{
|
|
|
|
case T_ELIF:
|
|
|
|
case T_ELSE:
|
|
|
|
case T_ENDIF:
|
|
|
|
case T_IF:
|
2021-01-07 09:48:07 -08:00
|
|
|
case T_IFDEF:
|
2021-01-07 09:48:08 -08:00
|
|
|
case T_IFNDEF:
|
2021-01-07 09:48:06 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (!preproc_is_writing(ctx))
|
|
|
|
continue;
|
|
|
|
}
|
2020-12-21 12:37:06 -08:00
|
|
|
|
2021-01-14 12:47:49 -08:00
|
|
|
if (ctx->current_directive == T_PRAGMA)
|
|
|
|
{
|
|
|
|
/* Print all tokens verbatim. */
|
|
|
|
if (token == T_PRAGMA)
|
|
|
|
vkd3d_string_buffer_printf(&ctx->buffer, "#pragma ");
|
|
|
|
else
|
|
|
|
vkd3d_string_buffer_printf(&ctx->buffer, "%s", text);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:14:19 -08:00
|
|
|
switch (func_state->state)
|
2021-01-12 14:14:17 -08:00
|
|
|
{
|
2021-01-12 14:14:19 -08:00
|
|
|
case STATE_NONE:
|
2021-01-12 14:14:20 -08:00
|
|
|
if (token == T_CONCAT && should_concat(ctx))
|
|
|
|
{
|
|
|
|
while (ctx->buffer.content_size
|
|
|
|
&& strchr(" \t\r\n", ctx->buffer.buffer[ctx->buffer.content_size - 1]))
|
|
|
|
--ctx->buffer.content_size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-29 16:08:29 -07:00
|
|
|
if (token == T_HASHSTRING)
|
2021-01-12 14:14:21 -08:00
|
|
|
{
|
2023-07-29 16:14:23 -07:00
|
|
|
struct vkd3d_string_buffer buffer;
|
2021-01-12 14:14:21 -08:00
|
|
|
|
|
|
|
if (ctx->current_directive)
|
|
|
|
return return_token(token, lval, text);
|
|
|
|
|
2023-07-29 16:14:23 -07:00
|
|
|
vkd3d_string_buffer_init(&buffer);
|
|
|
|
preproc_stringify(ctx, &buffer, text);
|
|
|
|
vkd3d_string_buffer_printf(&ctx->buffer, "%s", buffer.buffer);
|
|
|
|
vkd3d_string_buffer_cleanup(&buffer);
|
2021-01-12 14:14:21 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:14:19 -08:00
|
|
|
if (token == T_IDENTIFIER || token == T_IDENTIFIER_PAREN)
|
|
|
|
{
|
|
|
|
const struct preproc_text *expansion;
|
|
|
|
struct preproc_macro *macro;
|
|
|
|
|
|
|
|
switch (ctx->current_directive)
|
|
|
|
{
|
|
|
|
case T_DEFINE:
|
|
|
|
case T_IFDEF:
|
|
|
|
case T_IFNDEF:
|
|
|
|
case T_UNDEF:
|
|
|
|
/* Return identifiers verbatim. */
|
|
|
|
return return_token(token, lval, text);
|
2021-01-14 12:47:51 -08:00
|
|
|
|
|
|
|
case T_IF:
|
|
|
|
case T_ELIF:
|
|
|
|
/* Return identifiers verbatim only if they're the
|
|
|
|
* argument to "defined". */
|
|
|
|
if (ctx->last_was_defined)
|
|
|
|
{
|
|
|
|
ctx->last_was_defined = false;
|
|
|
|
return return_token(token, lval, text);
|
|
|
|
}
|
|
|
|
break;
|
2021-01-12 14:14:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, expand a macro if there is one. */
|
|
|
|
|
|
|
|
if ((expansion = find_arg_expansion(ctx, text)))
|
|
|
|
{
|
|
|
|
preproc_push_expansion(ctx, expansion, NULL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((macro = preproc_find_macro(ctx, text)))
|
|
|
|
{
|
|
|
|
if (!macro->arg_count)
|
|
|
|
{
|
|
|
|
preproc_push_expansion(ctx, ¯o->body, macro);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
func_state->state = STATE_IDENTIFIER;
|
|
|
|
func_state->macro = macro;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2021-01-14 12:47:47 -08:00
|
|
|
|
2021-01-14 12:47:48 -08:00
|
|
|
if (!strcmp(text, "__FILE__"))
|
|
|
|
{
|
|
|
|
const struct preproc_file *file = preproc_get_top_file(ctx);
|
|
|
|
|
|
|
|
/* Not the current file name, but rather the file name
|
|
|
|
* before invoking any macros. */
|
|
|
|
|
|
|
|
if (ctx->current_directive)
|
|
|
|
{
|
|
|
|
char *string;
|
|
|
|
|
|
|
|
if (!(string = vkd3d_malloc(strlen(file->filename) + 3)))
|
|
|
|
return 0;
|
|
|
|
sprintf(string, "\"%s\"", file->filename);
|
|
|
|
lval->string = string;
|
|
|
|
return T_STRING;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preproc_is_writing(ctx))
|
|
|
|
vkd3d_string_buffer_printf(&ctx->buffer, "\"%s\" ", file->filename);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-14 12:47:47 -08:00
|
|
|
if (!strcmp(text, "__LINE__"))
|
|
|
|
{
|
|
|
|
const struct preproc_file *file = preproc_get_top_file(ctx);
|
|
|
|
|
|
|
|
/* Not the current line number, but rather the line
|
|
|
|
* number before invoking any macros. */
|
|
|
|
|
|
|
|
if (ctx->current_directive)
|
|
|
|
{
|
|
|
|
char string[13];
|
|
|
|
|
|
|
|
sprintf(string, "%d", file->buffer.location.line);
|
|
|
|
return return_token(T_INTEGER, lval, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preproc_is_writing(ctx))
|
|
|
|
vkd3d_string_buffer_printf(&ctx->buffer, "%d ", file->buffer.location.line);
|
|
|
|
continue;
|
|
|
|
}
|
2021-01-12 14:14:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->current_directive)
|
2021-01-12 14:14:17 -08:00
|
|
|
return return_token(token, lval, text);
|
|
|
|
|
2022-03-30 14:38:32 -07:00
|
|
|
if (isspace(text[0]))
|
|
|
|
vkd3d_string_buffer_printf(&ctx->buffer, "%s", text);
|
|
|
|
else
|
|
|
|
vkd3d_string_buffer_printf(&ctx->buffer, "%s ", text);
|
2021-01-12 14:14:19 -08:00
|
|
|
break;
|
2021-01-12 14:14:17 -08:00
|
|
|
|
2021-01-12 14:14:19 -08:00
|
|
|
case STATE_IDENTIFIER:
|
|
|
|
if (token == '(')
|
|
|
|
{
|
|
|
|
struct preproc_text *first_arg = &func_state->macro->arg_values[0];
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
func_state->arg_count = 0;
|
|
|
|
func_state->paren_depth = 1;
|
|
|
|
func_state->state = STATE_ARGS;
|
|
|
|
for (i = 0; i < func_state->macro->arg_count; ++i)
|
|
|
|
func_state->macro->arg_values[i].text.content_size = 0;
|
|
|
|
|
|
|
|
first_arg->location = *lloc;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *name = func_state->macro->name;
|
|
|
|
|
|
|
|
ctx->lookahead_token = token;
|
|
|
|
func_state->macro = NULL;
|
|
|
|
func_state->state = STATE_NONE;
|
|
|
|
|
|
|
|
if (ctx->current_directive)
|
|
|
|
return return_token(T_IDENTIFIER, lval, name);
|
|
|
|
|
|
|
|
vkd3d_string_buffer_printf(&ctx->buffer, "%s ", name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_ARGS:
|
2021-01-12 14:14:17 -08:00
|
|
|
{
|
2021-01-12 14:14:19 -08:00
|
|
|
struct preproc_text *current_arg = NULL;
|
|
|
|
|
2024-08-02 01:34:34 -07:00
|
|
|
VKD3D_ASSERT(func_state->macro->arg_count);
|
2021-01-12 14:14:19 -08:00
|
|
|
|
|
|
|
if (func_state->arg_count < func_state->macro->arg_count)
|
|
|
|
current_arg = &func_state->macro->arg_values[func_state->arg_count];
|
|
|
|
|
|
|
|
switch (token)
|
|
|
|
{
|
2023-07-29 16:13:41 -07:00
|
|
|
/* Most text gets left alone (e.g. if it contains macros,
|
|
|
|
* the macros should be evaluated later).
|
|
|
|
* Arguments are a special case, and are replaced with
|
|
|
|
* their values immediately. */
|
|
|
|
case T_IDENTIFIER:
|
|
|
|
case T_IDENTIFIER_PAREN:
|
|
|
|
{
|
|
|
|
const struct preproc_text *expansion;
|
|
|
|
|
|
|
|
if ((expansion = find_arg_expansion(ctx, text)))
|
|
|
|
{
|
|
|
|
preproc_push_expansion(ctx, expansion, NULL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_arg)
|
|
|
|
preproc_text_add(current_arg, text);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-29 16:15:14 -07:00
|
|
|
/* Stringification is another special case. Unsurprisingly,
|
|
|
|
* we need to stringify if this is an argument. More
|
|
|
|
* surprisingly, we need to stringify even if it's not. */
|
|
|
|
case T_HASHSTRING:
|
|
|
|
{
|
|
|
|
struct vkd3d_string_buffer buffer;
|
|
|
|
|
|
|
|
vkd3d_string_buffer_init(&buffer);
|
|
|
|
preproc_stringify(ctx, &buffer, text);
|
|
|
|
if (current_arg)
|
|
|
|
preproc_text_add(current_arg, buffer.buffer);
|
|
|
|
vkd3d_string_buffer_cleanup(&buffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:14:19 -08:00
|
|
|
case T_NEWLINE:
|
|
|
|
if (current_arg)
|
|
|
|
preproc_text_add(current_arg, " ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ')':
|
|
|
|
case ']':
|
|
|
|
case '}':
|
|
|
|
if (!--func_state->paren_depth)
|
|
|
|
{
|
|
|
|
if (++func_state->arg_count == func_state->macro->arg_count)
|
|
|
|
{
|
|
|
|
preproc_push_expansion(ctx, &func_state->macro->body, func_state->macro);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
preproc_warning(ctx, lloc, VKD3D_SHADER_WARNING_PP_ARGUMENT_COUNT_MISMATCH,
|
|
|
|
"Wrong number of arguments to macro \"%s\": expected %zu, got %zu.",
|
|
|
|
func_state->macro->name, func_state->macro->arg_count, func_state->arg_count);
|
|
|
|
|
|
|
|
if (ctx->current_directive)
|
|
|
|
return return_token(T_IDENTIFIER, lval, func_state->macro->name);
|
|
|
|
|
|
|
|
vkd3d_string_buffer_printf(&ctx->buffer, "%s ", func_state->macro->name);
|
|
|
|
}
|
|
|
|
func_state->macro = NULL;
|
|
|
|
func_state->state = STATE_NONE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (current_arg)
|
|
|
|
preproc_text_add(current_arg, text);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ',':
|
|
|
|
if (func_state->paren_depth == 1)
|
|
|
|
{
|
|
|
|
++func_state->arg_count;
|
|
|
|
if (current_arg)
|
|
|
|
current_arg->location = *lloc;
|
|
|
|
}
|
|
|
|
else if (current_arg)
|
|
|
|
{
|
|
|
|
preproc_text_add(current_arg, text);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
case '[':
|
|
|
|
case '{':
|
|
|
|
++func_state->paren_depth;
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (current_arg)
|
|
|
|
preproc_text_add(current_arg, text);
|
|
|
|
}
|
2023-07-29 16:17:35 -07:00
|
|
|
|
|
|
|
if (current_arg)
|
|
|
|
preproc_text_add(current_arg, " ");
|
2021-01-12 14:14:19 -08:00
|
|
|
break;
|
2021-01-12 14:14:17 -08:00
|
|
|
}
|
|
|
|
}
|
2020-12-07 10:56:30 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 09:48:09 -08:00
|
|
|
bool preproc_push_include(struct preproc_ctx *ctx, char *filename, const struct vkd3d_shader_code *code)
|
|
|
|
{
|
|
|
|
struct preproc_file *file;
|
|
|
|
|
|
|
|
if (!vkd3d_array_reserve((void **)&ctx->file_stack, &ctx->file_stack_size,
|
|
|
|
ctx->file_count + 1, sizeof(*ctx->file_stack)))
|
|
|
|
return false;
|
|
|
|
file = &ctx->file_stack[ctx->file_count++];
|
|
|
|
memset(file, 0, sizeof(*file));
|
|
|
|
file->code = *code;
|
|
|
|
file->filename = filename;
|
|
|
|
file->buffer.lexer_buffer = yy_scan_bytes(code->code, code->size, ctx->scanner);
|
|
|
|
file->buffer.location.source_name = file->filename;
|
|
|
|
file->buffer.location.line = 1;
|
|
|
|
file->buffer.location.column = 1;
|
|
|
|
TRACE("File stack size is now %zu.\n", ctx->file_count);
|
|
|
|
ctx->last_was_newline = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-07 09:48:06 -08:00
|
|
|
static int preproc_macro_compare(const void *key, const struct rb_entry *entry)
|
|
|
|
{
|
|
|
|
const struct preproc_macro *macro = RB_ENTRY_VALUE(entry, struct preproc_macro, entry);
|
|
|
|
const char *name = key;
|
|
|
|
|
|
|
|
return strcmp(name, macro->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void preproc_macro_rb_free(struct rb_entry *entry, void *ctx)
|
|
|
|
{
|
|
|
|
preproc_free_macro(RB_ENTRY_VALUE(entry, struct preproc_macro, entry));
|
|
|
|
}
|
|
|
|
|
2020-12-07 10:56:30 -08:00
|
|
|
int preproc_lexer_parse(const struct vkd3d_shader_compile_info *compile_info,
|
|
|
|
struct vkd3d_shader_code *out, struct vkd3d_shader_message_context *message_context)
|
|
|
|
{
|
2021-01-07 09:48:09 -08:00
|
|
|
static const struct vkd3d_shader_preprocess_info default_preprocess_info = {0};
|
2020-12-07 10:56:30 -08:00
|
|
|
struct preproc_ctx ctx = {0};
|
2021-01-25 09:23:57 -08:00
|
|
|
char *source_name = NULL;
|
2020-12-07 10:56:30 -08:00
|
|
|
void *output_code;
|
2021-01-25 09:23:57 -08:00
|
|
|
unsigned int i;
|
2020-12-07 10:56:30 -08:00
|
|
|
|
|
|
|
vkd3d_string_buffer_init(&ctx.buffer);
|
2021-01-07 09:48:06 -08:00
|
|
|
rb_init(&ctx.macros, preproc_macro_compare);
|
2021-01-07 09:48:09 -08:00
|
|
|
if (!(ctx.preprocess_info = vkd3d_find_struct(compile_info->next, PREPROCESS_INFO)))
|
|
|
|
ctx.preprocess_info = &default_preprocess_info;
|
2020-12-15 15:13:20 -08:00
|
|
|
ctx.message_context = message_context;
|
2021-01-07 09:48:09 -08:00
|
|
|
|
|
|
|
if (!(source_name = vkd3d_strdup(compile_info->source_name ? compile_info->source_name : "<anonymous>")))
|
2021-01-25 09:23:57 -08:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
for (i = 0; i < ctx.preprocess_info->macro_count; ++i)
|
2021-01-07 09:48:09 -08:00
|
|
|
{
|
2021-01-25 09:23:57 -08:00
|
|
|
const struct vkd3d_shader_location loc = {.source_name = source_name};
|
|
|
|
struct vkd3d_string_buffer body;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
vkd3d_string_buffer_init(&body);
|
|
|
|
vkd3d_string_buffer_printf(&body, "%s", ctx.preprocess_info->macros[i].value);
|
|
|
|
if (!(name = vkd3d_strdup(ctx.preprocess_info->macros[i].name)))
|
2021-01-26 11:26:33 -08:00
|
|
|
{
|
|
|
|
vkd3d_string_buffer_cleanup(&body);
|
2021-01-25 09:23:57 -08:00
|
|
|
goto fail;
|
2021-01-26 11:26:33 -08:00
|
|
|
}
|
2021-01-25 09:23:57 -08:00
|
|
|
if (!preproc_add_macro(&ctx, &loc, name, NULL, 0, &loc, &body))
|
|
|
|
{
|
|
|
|
vkd3d_free(name);
|
2021-01-26 11:26:33 -08:00
|
|
|
vkd3d_string_buffer_cleanup(&body);
|
2021-01-25 09:23:57 -08:00
|
|
|
goto fail;
|
|
|
|
}
|
2021-01-07 09:48:09 -08:00
|
|
|
}
|
2020-12-07 10:56:30 -08:00
|
|
|
|
|
|
|
yylex_init_extra(&ctx, &ctx.scanner);
|
2021-01-07 09:48:09 -08:00
|
|
|
if (!preproc_push_include(&ctx, source_name, &compile_info->source))
|
|
|
|
{
|
|
|
|
yylex_destroy(ctx.scanner);
|
2021-01-25 09:23:57 -08:00
|
|
|
goto fail;
|
2021-01-07 09:48:09 -08:00
|
|
|
}
|
2020-12-07 10:56:30 -08:00
|
|
|
|
|
|
|
preproc_yyparse(ctx.scanner, &ctx);
|
|
|
|
|
2021-01-12 14:14:19 -08:00
|
|
|
switch (ctx.text_func.state)
|
|
|
|
{
|
|
|
|
case STATE_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_ARGS:
|
|
|
|
{
|
|
|
|
const struct vkd3d_shader_location loc = {.source_name = source_name};
|
|
|
|
|
|
|
|
preproc_warning(&ctx, &loc, VKD3D_SHADER_WARNING_PP_UNTERMINATED_MACRO,
|
|
|
|
"Unterminated macro invocation.");
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
case STATE_IDENTIFIER:
|
|
|
|
if (preproc_is_writing(&ctx))
|
|
|
|
vkd3d_string_buffer_printf(&ctx.buffer, "%s ", ctx.text_func.macro->name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-07 09:48:09 -08:00
|
|
|
while (ctx.file_count)
|
|
|
|
preproc_pop_buffer(&ctx);
|
2020-12-07 10:56:30 -08:00
|
|
|
yylex_destroy(ctx.scanner);
|
|
|
|
|
2021-01-07 09:48:06 -08:00
|
|
|
rb_destroy(&ctx.macros, preproc_macro_rb_free, NULL);
|
2021-01-07 09:48:09 -08:00
|
|
|
vkd3d_free(ctx.file_stack);
|
2021-01-12 14:14:17 -08:00
|
|
|
vkd3d_free(ctx.expansion_stack);
|
2020-12-21 12:37:06 -08:00
|
|
|
|
2020-12-15 15:13:20 -08:00
|
|
|
if (ctx.error)
|
|
|
|
{
|
|
|
|
WARN("Failed to preprocess.\n");
|
|
|
|
vkd3d_string_buffer_cleanup(&ctx.buffer);
|
|
|
|
return VKD3D_ERROR_INVALID_SHADER;
|
|
|
|
}
|
|
|
|
|
2020-12-07 10:56:30 -08:00
|
|
|
if (!(output_code = vkd3d_malloc(ctx.buffer.content_size)))
|
|
|
|
{
|
|
|
|
vkd3d_string_buffer_cleanup(&ctx.buffer);
|
|
|
|
return VKD3D_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(output_code, ctx.buffer.buffer, ctx.buffer.content_size);
|
|
|
|
out->size = ctx.buffer.content_size;
|
|
|
|
out->code = output_code;
|
|
|
|
vkd3d_string_buffer_trace(&ctx.buffer);
|
|
|
|
vkd3d_string_buffer_cleanup(&ctx.buffer);
|
|
|
|
return VKD3D_OK;
|
2021-01-25 09:23:57 -08:00
|
|
|
|
|
|
|
fail:
|
|
|
|
rb_destroy(&ctx.macros, preproc_macro_rb_free, NULL);
|
|
|
|
vkd3d_free(source_name);
|
|
|
|
vkd3d_string_buffer_cleanup(&ctx.buffer);
|
|
|
|
return VKD3D_ERROR_OUT_OF_MEMORY;
|
2020-12-07 10:56:30 -08:00
|
|
|
}
|