From 4bc7b9243aeb6dc554d1fe506759c74c6dab4e1d Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 9 Nov 2014 13:01:35 -0800 Subject: [PATCH] Define 'this' as a keyword. --- sourcepawn/compiler/sc.h | 1 + sourcepawn/compiler/sc1.cpp | 3 ++- sourcepawn/compiler/sc2.cpp | 2 +- sourcepawn/compiler/sc3.cpp | 18 +++++++++++++++++- sourcepawn/compiler/sc5-in.scp | 1 + .../tests/fail-array-on-implicit-this.sp | 2 +- .../compiler/tests/fail-assign-to-this.sp | 12 ++++++++++++ .../compiler/tests/fail-assign-to-this.txt | 1 + .../compiler/tests/fail-dtor-extra-args.sp | 2 +- .../compiler/tests/fail-dtor-non-native.sp | 2 +- .../compiler/tests/fail-method-on-array.sp | 2 +- .../compiler/tests/fail-method-on-function.sp | 2 +- .../tests/fail-mismatch-on-implicit-this.sp | 2 +- .../tests/fail-multi-tag-on-implicit-this.sp | 2 +- .../compiler/tests/fail-this-outside-method.sp | 3 +++ .../tests/fail-this-outside-method.txt | 1 + .../compiler/tests/ok-base-type-as-thistag.sp | 2 +- sourcepawn/compiler/tests/ok-inheritance.sp | 2 +- .../compiler/tests/ok-method-on-const.sp | 2 +- .../compiler/tests/ok-method-on-constref.sp | 2 +- .../compiler/tests/ok-method-on-element.sp | 2 +- sourcepawn/compiler/tests/ok-method-on-ref.sp | 2 +- .../compiler/tests/ok-method-on-scalar.sp | 4 ++-- sourcepawn/compiler/tests/warn-bad-upcast.sp | 2 +- 24 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 sourcepawn/compiler/tests/fail-assign-to-this.sp create mode 100644 sourcepawn/compiler/tests/fail-assign-to-this.txt create mode 100644 sourcepawn/compiler/tests/fail-this-outside-method.sp create mode 100644 sourcepawn/compiler/tests/fail-this-outside-method.txt diff --git a/sourcepawn/compiler/sc.h b/sourcepawn/compiler/sc.h index 1c02741f..9e200699 100644 --- a/sourcepawn/compiler/sc.h +++ b/sourcepawn/compiler/sc.h @@ -434,6 +434,7 @@ enum TokenKind { tSWITCH, tTAGOF, tTHEN, + tTHIS, tTYPEDEF, tUNION, tVOID, diff --git a/sourcepawn/compiler/sc1.cpp b/sourcepawn/compiler/sc1.cpp index 796226ff..edeb8daf 100644 --- a/sourcepawn/compiler/sc1.cpp +++ b/sourcepawn/compiler/sc1.cpp @@ -3702,7 +3702,6 @@ int check_this_tag(methodmap_t *map, symbol *target) const arginfo *first_arg = &target->dim.arglist[0]; if (first_arg->ident == 0 || first_arg->ident != iVARIABLE || - (first_arg->usage & uCONST) || first_arg->hasdefault || first_arg->numtags != 1) { @@ -5576,6 +5575,7 @@ static int declargs(symbol *sym, int chkshadow, const int *thistag) argptr->tags = (int *)malloc(sizeof(int)); argptr->tags[0] = *thistag; argptr->numtags = 1; + argptr->usage = uCONST; } else { argptr = &sym->dim.arglist[0]; } @@ -5591,6 +5591,7 @@ static int declargs(symbol *sym, int chkshadow, const int *thistag) argptr->idxtag, 0 ); + sym->usage |= uCONST; markusage(sym, uREAD); argcnt++; diff --git a/sourcepawn/compiler/sc2.cpp b/sourcepawn/compiler/sc2.cpp index d02a082a..c0119e24 100644 --- a/sourcepawn/compiler/sc2.cpp +++ b/sourcepawn/compiler/sc2.cpp @@ -1973,7 +1973,7 @@ const char *sc_tokens[] = { "public", "return", "sizeof", "sleep", "static", "stock", "struct", "switch", - "tagof", "*then", "typedef", + "tagof", "*then", "this", "typedef", "union", "void", "while", diff --git a/sourcepawn/compiler/sc3.cpp b/sourcepawn/compiler/sc3.cpp index 1a067b6b..5fe2878c 100644 --- a/sourcepawn/compiler/sc3.cpp +++ b/sourcepawn/compiler/sc3.cpp @@ -2304,12 +2304,28 @@ static int primary(value *lval) clear_value(lval); /* clear lval */ tok=lex(&val,&st); + + if (tok == tTHIS) { + strcpy(lastsymbol, "this"); + if ((sym = findloc("this")) == NULL) { + error(166); /* 'this' outside method body */ + ldconst(0, sPRI); + return FALSE; + } + + assert(sym->ident == iVARIABLE); + lval->sym = sym; + lval->ident = sym->ident; + lval->tag = sym->tag; + return TRUE; + } + if (tok==tSYMBOL) { /* lastsymbol is char[sNAMEMAX+1], lex() should have truncated any symbol * to sNAMEMAX significant characters */ assert(strlen(st)