Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/compiler/sc.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ enum {
tFORWARD,
tGOTO,
tIF,
t__NAMEOF,
tNATIVE,
tNEW,
tOPERATOR,
Expand Down
5 changes: 3 additions & 2 deletions source/compiler/sc2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2172,8 +2172,9 @@ char *sc_tokens[] = {
"...", "..",
"__addressof", "assert", "*begin", "break", "case", "char", "const", "continue",
"default", "defined", "do", "else", "__emit", "*end", "enum", "exit", "for",
"forward", "goto", "if", "native", "new", "operator", "public", "return", "sizeof",
"sleep", "state", "static", "stock", "switch", "tagof", "*then", "while",
"forward", "goto", "if", "__nameof", "native", "new", "operator", "public",
"return", "sizeof", "sleep", "state", "static", "stock", "switch", "tagof",
"*then", "while",
"#assert", "#define", "#else", "#elseif", "#emit", "#endif", "#endinput",
"#endscript", "#error", "#file", "#if", "#include", "#line", "#pragma",
"#tryinclude", "#undef", "#warning",
Expand Down
24 changes: 24 additions & 0 deletions source/compiler/sc3.c
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,30 @@ static int hier2(value *lval)
while (paranthese--)
needtoken(')');
return FALSE;
case t__NAMEOF:
paranthese = 0;
while (matchtoken('('))
paranthese++;
tok=lex(&val, &st);
ldconst((litidx +glb_declared)*sizeof(cell),sPRI);
if (tok!=tSYMBOL)
return error_suggest(20, st, NULL, estNONSYMBOL, tok); /* illegal symbol name */
sym = findloc(st);
if (sym==NULL)
sym=findglb(st, sSTATEVAR);
if (sym==NULL)
return error_suggest(17, st, NULL, estSYMBOL, esfVARCONST); /* undefined symbol */
else if ((sym->usage & uDEFINE)==0)
return error_suggest(17, st, NULL, estSYMBOL, esfVARCONST); /* undefined symbol (symbol is in the table, but it is "used" only) */
clear_value(lval);
for (tag=0; sym->name[tag]; ++tag)
litadd(sym->name[tag]);
litadd(0);
lval->ident=iARRAY;
lval->constval=-1-tag;
while (paranthese--)
needtoken(')');
return FALSE;
case tSIZEOF:
paranthese=0;
while (matchtoken('('))
Expand Down
5 changes: 5 additions & 0 deletions source/compiler/tests/__nameof_1.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
'test_type': 'output_check',
'errors': """
"""
}
38 changes: 38 additions & 0 deletions source/compiler/tests/__nameof_1.pwn
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const SYMBOL_1 = 4;

new symbol_2 = 4;

#pragma unused symbol_2

enum SYMBOL_3
{
SYMBOL_4,
};

UnusedFunc()
{
return 5;
}

#pragma unused UnusedFunc

TestFunc(local_1, const local_2[])
{
#pragma unused local_1, local_2
new dest[32];
dest = __nameof(SYMBOL_1);
dest = __nameof(symbol_2);
dest = __nameof(SYMBOL_3);
dest = __nameof(SYMBOL_4);
dest = __nameof(local_1);
dest = __nameof(local_2);
dest = __nameof(dest);
dest = __nameof(TestFunc);
dest = __nameof(UnusedFunc);
}

main()
{
TestFunc(0, "__nameof");
}

16 changes: 16 additions & 0 deletions source/compiler/tests/__nameof_2.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
'test_type': 'output_check',
'errors': """
__nameof_2.pwn(7) : error 017: undefined symbol "main"
__nameof_2.pwn(7) : error 029: invalid expression, assumed zero
__nameof_2.pwn(7) : warning 215: expression has no effect
__nameof_2.pwn(9) : error 017: undefined symbol "DoesntExist"
__nameof_2.pwn(9) : error 029: invalid expression, assumed zero
__nameof_2.pwn(9) : warning 215: expression has no effect
__nameof_2.pwn(10) : error 017: undefined symbol "local_3"; did you mean "local_1"?
__nameof_2.pwn(10) : error 029: invalid expression, assumed zero
__nameof_2.pwn(10) : warning 215: expression has no effect
__nameof_2.pwn(3) : warning 203: symbol is never used: "local_1"
__nameof_2.pwn(1) : warning 203: symbol is never used: "symbol_1"
"""
}
17 changes: 17 additions & 0 deletions source/compiler/tests/__nameof_2.pwn
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
new symbol_1 = 4;

TestFunc(local_1)
{
new dest[32];
dest = __nameof(symbol_1);
dest = __nameof(main);
dest = __nameof(local_1);
dest = __nameof(DoesntExist);
dest = __nameof(local_3);
}

main()
{
TestFunc(0);
}