From 68e06458132e8cbdebc3167642279c2020ba7d92 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 30 Nov 2014 19:08:25 -0800 Subject: [PATCH] Add a view_as operator. --- sourcepawn/compiler/sc.h | 1 + sourcepawn/compiler/sc1.cpp | 3 +-- sourcepawn/compiler/sc3.cpp | 27 +++++++++++++++++++ .../tests/fail-cannot-view-as-void.sp | 3 +++ .../tests/fail-cannot-view-as-void.txt | 1 + .../compiler/tests/ok-view-int-as-float.sp | 3 +++ 6 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 sourcepawn/compiler/tests/fail-cannot-view-as-void.sp create mode 100644 sourcepawn/compiler/tests/fail-cannot-view-as-void.txt create mode 100644 sourcepawn/compiler/tests/ok-view-int-as-float.sp diff --git a/sourcepawn/compiler/sc.h b/sourcepawn/compiler/sc.h index 07f3d444..63912f0c 100644 --- a/sourcepawn/compiler/sc.h +++ b/sourcepawn/compiler/sc.h @@ -562,6 +562,7 @@ int pc_enablewarning(int number,int enable); const char *pc_tagname(int tag); int parse_decl(declinfo_t *decl, int flags); const char *type_to_name(int tag); +bool parse_new_typename(const token_t *tok, int *tagp); /* * Functions called from the compiler (to be implemented by you) diff --git a/sourcepawn/compiler/sc1.cpp b/sourcepawn/compiler/sc1.cpp index c2be2ccb..aad91220 100644 --- a/sourcepawn/compiler/sc1.cpp +++ b/sourcepawn/compiler/sc1.cpp @@ -157,7 +157,6 @@ static void inst_datetime_defines(void); static void inst_binary_name(char *binfname); static int operatorname(char *name); static int parse_new_typename(const token_t *tok); -static bool parse_new_typename(const token_t *tok, int *outp); static int parse_new_decl(declinfo_t *decl, const token_t *first, int flags); static int reparse_old_decl(declinfo_t *decl, int flags); static int reparse_new_decl(declinfo_t *decl, int flags); @@ -3116,7 +3115,7 @@ static int parse_new_typename(const token_t *tok) return -1; } -static bool parse_new_typename(const token_t *tok, int *tagp) +bool parse_new_typename(const token_t *tok, int *tagp) { int tag = parse_new_typename(tok); if (tag >= 0) diff --git a/sourcepawn/compiler/sc3.cpp b/sourcepawn/compiler/sc3.cpp index 6f3c7977..ca163ada 100644 --- a/sourcepawn/compiler/sc3.cpp +++ b/sourcepawn/compiler/sc3.cpp @@ -1660,6 +1660,33 @@ static int hier2(value *lval) callfunction(target, NULL, lval, TRUE); return FALSE; } + case tVIEW_AS: /* newer tagname override */ + { + needtoken('<'); + int tag = 0; + { + token_t tok; + lextok(&tok); + if (!parse_new_typename(&tok, &tag)) + tag = 0; + } + needtoken('>'); + + if (tag == pc_tag_void) + error(144); + + lval->cmptag = tag; + lvalue = hier12(lval); + + if ((lval->tag & OBJECTTAG) || (tag & OBJECTTAG)) { + matchtag(tag, lval->tag, MATCHTAG_COERCE); + } else if ((tag & FUNCTAG) != (lval->tag & FUNCTAG)) { + // Warn: unsupported cast. + error(237); + } + lval->tag = tag; + return lvalue; + } case tLABEL: /* tagname override */ tag=pc_addtag(st); lval->cmptag=tag; diff --git a/sourcepawn/compiler/tests/fail-cannot-view-as-void.sp b/sourcepawn/compiler/tests/fail-cannot-view-as-void.sp new file mode 100644 index 00000000..bec4adf4 --- /dev/null +++ b/sourcepawn/compiler/tests/fail-cannot-view-as-void.sp @@ -0,0 +1,3 @@ +public float egg() { + return view_as(10); +} diff --git a/sourcepawn/compiler/tests/fail-cannot-view-as-void.txt b/sourcepawn/compiler/tests/fail-cannot-view-as-void.txt new file mode 100644 index 00000000..c4631319 --- /dev/null +++ b/sourcepawn/compiler/tests/fail-cannot-view-as-void.txt @@ -0,0 +1 @@ +(2) : error 144: void cannot be used as a variable type diff --git a/sourcepawn/compiler/tests/ok-view-int-as-float.sp b/sourcepawn/compiler/tests/ok-view-int-as-float.sp new file mode 100644 index 00000000..74ce7a1d --- /dev/null +++ b/sourcepawn/compiler/tests/ok-view-int-as-float.sp @@ -0,0 +1,3 @@ +public float egg() { + return view_as(10); +}