summaryrefslogtreecommitdiff
path: root/parse
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2017-07-30 18:34:52 -0700
committerOri Bernstein <ori@eigenstate.org>2017-07-30 18:40:02 -0700
commit594223c2959cc116ccc2782ebf7930677b053843 (patch)
tree82781e9646db2e51b0f096753fb49533fa8d7c2c /parse
parent093716ff5e2ffe837793ba7c9239c64e22789ec0 (diff)
downloadmc-594223c2959cc116ccc2782ebf7930677b053843.tar.gz
Add hook into frontend for sizeof() folding.
Before, code like: const f = { var buf: byte[sizeof(int)] } would crash us, because `sizeof` would not get folded. A slightly subtle point: muse doens't install a hook here, but since it's already handling processed usefiles, the array sizes should already be prefolded for its viewing pleasure.
Diffstat (limited to 'parse')
-rw-r--r--parse/fold.c6
-rw-r--r--parse/infer.c3
-rw-r--r--parse/parse.h1
3 files changed, 10 insertions, 0 deletions
diff --git a/parse/fold.c b/parse/fold.c
index d01ff6a..c13b41c 100644
--- a/parse/fold.c
+++ b/parse/fold.c
@@ -13,6 +13,8 @@
#include "util.h"
#include "parse.h"
+size_t (*sizefn)(Node *n);
+
static int
getintlit(Node *n, vlong *v)
{
@@ -250,6 +252,10 @@ fold(Node *n, int foldvar)
case Ocast:
r = foldcast(n);
break;
+ case Osize:
+ if (sizefn)
+ r = val(n->loc, sizefn(n->expr.args[0]), exprtype(n));
+ break;
default:
break;
}
diff --git a/parse/infer.c b/parse/infer.c
index fc9b86b..d1465ee 100644
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -2031,6 +2031,9 @@ tyfix(Node *ctx, Type *orig, int noerr)
t->fixed = 1;
if (t->type == Tyarray) {
typesub(t->asize, noerr);
+ t->asize = fold(t->asize, 1);
+ if (exprop(t->asize) != Olit)
+ fatal(t->asize, "nonconstant array size near %s\n", ctxstr(t->asize));
} else if (t->type == Tystruct) {
inaggr++;
for (i = 0; i < t->nmemb; i++)
diff --git a/parse/parse.h b/parse/parse.h
index 97198a8..74abae4 100644
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -573,5 +573,6 @@ extern char *objdir;
extern char **incpaths;
extern size_t nincpaths;
extern char *localincpath;
+extern size_t (*sizefn)(Node *n);
void yyerror(const char *s);