mirror of
https://github.com/DarkPlacesEngine/gmqcc.git
synced 2025-01-18 14:21:36 +00:00
Implemented generalized attribute aliases. You can now alias functions, locals, or globals with [[alias("old_name")]] type new_name;
This commit is contained in:
parent
52ffc6db10
commit
e464cc1ea6
4 changed files with 93 additions and 7 deletions
1
ast.h
1
ast.h
|
@ -151,6 +151,7 @@ typedef struct
|
|||
#define AST_FLAG_DEPRECATED (1<<4)
|
||||
#define AST_FLAG_INCLUDE_DEF (1<<5)
|
||||
#define AST_FLAG_IS_VARARG (1<<6)
|
||||
#define AST_FLAG_ALIAS (1<<7)
|
||||
#define AST_FLAG_TYPE_MASK (AST_FLAG_VARIADIC | AST_FLAG_NORETURN)
|
||||
|
||||
/* Value
|
||||
|
|
72
parser.c
72
parser.c
|
@ -62,6 +62,7 @@ typedef struct {
|
|||
size_t crc_fields;
|
||||
|
||||
ast_function *function;
|
||||
ht aliases;
|
||||
|
||||
/* All the labels the function defined...
|
||||
* Should they be in ast_function instead?
|
||||
|
@ -1825,9 +1826,11 @@ static bool parse_sya_operand(parser_t *parser, shunt *sy, bool with_labels)
|
|||
/* intrinsics */
|
||||
if (!strcmp(parser_tokval(parser), "__builtin_debug_typestring")) {
|
||||
var = (ast_expression*)intrinsic_debug_typestring;
|
||||
} else {
|
||||
var = (ast_expression*)parser_find_var(parser, (const char *)util_htget(parser->aliases, parser_tokval(parser)));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if (!var) {
|
||||
char *correct = NULL;
|
||||
size_t i;
|
||||
|
||||
|
@ -2932,8 +2935,44 @@ static bool parse_qualifiers(parser_t *parser, bool with_local, int *cvq, bool *
|
|||
return false;
|
||||
}
|
||||
}
|
||||
else if (!strcmp(parser_tokval(parser), "alias") && !(flags & AST_FLAG_ALIAS)) {
|
||||
flags |= AST_FLAG_ALIAS;
|
||||
*message = NULL;
|
||||
|
||||
if (!parser_next(parser)) {
|
||||
parseerror(parser, "parse error in attribute");
|
||||
goto argerr;
|
||||
}
|
||||
|
||||
if (parser->tok == '(') {
|
||||
if (!parser_next(parser) || parser->tok != TOKEN_STRINGCONST) {
|
||||
parseerror(parser, "`alias` attribute missing parameter");
|
||||
goto argerr;
|
||||
}
|
||||
|
||||
*message = util_strdup(parser_tokval(parser));
|
||||
|
||||
if (!parser_next(parser)) {
|
||||
parseerror(parser, "parse error in attribute");
|
||||
goto argerr;
|
||||
}
|
||||
|
||||
if (parser->tok != ')') {
|
||||
parseerror(parser, "`alias` attribute expected `)` after parameter");
|
||||
goto argerr;
|
||||
}
|
||||
|
||||
if (!parser_next(parser)) {
|
||||
parseerror(parser, "parse error in attribute");
|
||||
goto argerr;
|
||||
}
|
||||
}
|
||||
|
||||
if (parser->tok != TOKEN_ATTRIBUTE_CLOSE) {
|
||||
parseerror(parser, "`alias` attribute expected `]]`");
|
||||
goto argerr;
|
||||
}
|
||||
}
|
||||
else if (!strcmp(parser_tokval(parser), "deprecated") && !(flags & AST_FLAG_DEPRECATED)) {
|
||||
flags |= AST_FLAG_DEPRECATED;
|
||||
*message = NULL;
|
||||
|
@ -3820,6 +3859,11 @@ static bool parse_function_body(parser_t *parser, ast_value *var)
|
|||
has_frame_think = false;
|
||||
old = parser->function;
|
||||
|
||||
if (var->expression.flags & AST_FLAG_ALIAS) {
|
||||
parseerror(parser, "function aliases cannot have bodies");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (vec_size(parser->gotos) || vec_size(parser->labels)) {
|
||||
parseerror(parser, "gotos/labels leaking");
|
||||
return false;
|
||||
|
@ -5012,7 +5056,13 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
|
|||
|
||||
var->cvq = qualifier;
|
||||
var->expression.flags |= qflags;
|
||||
if (var->expression.flags & AST_FLAG_DEPRECATED)
|
||||
|
||||
/*
|
||||
* store the vstring back to var for alias and
|
||||
* deprecation messages.
|
||||
*/
|
||||
if (var->expression.flags & AST_FLAG_DEPRECATED ||
|
||||
var->expression.flags & AST_FLAG_ALIAS)
|
||||
var->desc = vstring;
|
||||
|
||||
/* Part 1:
|
||||
|
@ -5217,11 +5267,15 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
|
|||
}
|
||||
}
|
||||
else {
|
||||
parser_addglobal(parser, var->name, (ast_expression*)var);
|
||||
if (isvector) {
|
||||
for (i = 0; i < 3; ++i) {
|
||||
parser_addglobal(parser, me[i]->name, (ast_expression*)me[i]);
|
||||
if (!(var->expression.flags & AST_FLAG_ALIAS)) {
|
||||
parser_addglobal(parser, var->name, (ast_expression*)var);
|
||||
if (isvector) {
|
||||
for (i = 0; i < 3; ++i) {
|
||||
parser_addglobal(parser, me[i]->name, (ast_expression*)me[i]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
util_htset(parser->aliases, var->name, (void*)var->desc);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -5718,6 +5772,8 @@ bool parser_init()
|
|||
vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
|
||||
vec_push(parser->_blocktypedefs, 0);
|
||||
|
||||
parser->aliases = util_htnew(PARSER_HT_SIZE);
|
||||
|
||||
/* corrector */
|
||||
vec_push(parser->correct_variables, correct_trie_new());
|
||||
vec_push(parser->correct_variables_score, NULL);
|
||||
|
@ -5868,6 +5924,8 @@ void parser_cleanup()
|
|||
ast_value_delete(parser->const_vec[1]);
|
||||
ast_value_delete(parser->const_vec[2]);
|
||||
|
||||
util_htdel(parser->aliases);
|
||||
|
||||
mem_d(parser);
|
||||
}
|
||||
|
||||
|
|
21
tests/aliases.qc
Normal file
21
tests/aliases.qc
Normal file
|
@ -0,0 +1,21 @@
|
|||
float alias_1 = 3.14;
|
||||
void alias_2() {
|
||||
print("alias_2\n");
|
||||
}
|
||||
|
||||
[[alias("alias_2")]] void alias_2_aliased();
|
||||
[[alias("alias_1")]] float alias_1_aliased;
|
||||
|
||||
// expected output
|
||||
// alias_2
|
||||
// 3.14
|
||||
void main() {
|
||||
alias_2_aliased();
|
||||
|
||||
print(
|
||||
ftos(
|
||||
alias_1_aliased
|
||||
),
|
||||
"\n"
|
||||
);
|
||||
}
|
6
tests/aliases.tmpl
Normal file
6
tests/aliases.tmpl
Normal file
|
@ -0,0 +1,6 @@
|
|||
I: aliases.qc
|
||||
D: test aliases
|
||||
T: -execute
|
||||
C: -std=gmqcc
|
||||
M: alias_2
|
||||
M: 3.14
|
Loading…
Reference in a new issue