commit 9e9cced8180d445ccaa8fc9452f9f39f63688dda
parent f547349114ecb059365e45a7a0fb9eee43b12745
Author: m21c <ho*******@gmail.com>
Date: Sat, 5 Feb 2022 13:16:00 +0100
add primitive()-macro for primitive types
Diffstat:
| M | compiler.c | | | 48 | +++++++++++++++++++++++++----------------------- |
1 file changed, 25 insertions(+), 23 deletions(-)
diff --git a/compiler.c b/compiler.c
@@ -517,6 +517,8 @@ Type prim[] = {
#undef entry
};
+#define primitive(typetag) (prim + typetag)
+
int keywordlengths[OSTART - KSTART];
const int keywordtypeids[] = {
@@ -996,26 +998,26 @@ tokenizealphanumeric(Source *source, register int ch)
static Type *
suffixfloattype(Source *source, const char *end)
{
- Type *ty = prim + TDOUBLE;
+ Type *ty = primitive(TDOUBLE);
if (*end == 0)
return ty;
/* FIXME(m21c): r-suffix might conflict with radix */
if ((*end == 'f' || *end == 'F') && !end[1]) {
- ty = prim + TFLOAT;
+ ty = primitive(TFLOAT);
} else if (*end == 'l' || *end == 'L') {
- ty = prim + TDOUBLE;
+ ty = primitive(TDOUBLE);
if (end[1])
goto errorfloat;
} else if (!mystrcasecmp(end, "f32") || !mystrcasecmp(end, "r32")) {
- ty = prim + TF32;
+ ty = primitive(TF32);
} else if (!mystrcasecmp(end, "f64") || !mystrcasecmp(end, "r64")) {
- ty = prim + TF64;
+ ty = primitive(TF64);
} else {
errorfloat:
@@ -1032,7 +1034,7 @@ suffixinttype(Source *source, const char *end)
switch (*end) {
case 0:
- return prim + TINFER;
+ return primitive(TINFER);
case 's': case 'S': case 'i': case 'I':
typeid = 0;
@@ -1081,7 +1083,7 @@ suffixinttype(Source *source, const char *end)
errorint:
error(&source->currloc, "invalid integer format");
- return prim + TINT;
+ return primitive(TINT);
}
static int
@@ -1128,7 +1130,7 @@ advancenum:
);
source->tok.u.u = 0;
- source->tok.type = prim + TINT;
+ source->tok.type = primitive(TINT);
return source->tok.kind = NUMBER;
}
@@ -1278,7 +1280,7 @@ skipwhite:
}
}
- source->tok.type = prim + TUNDEFINED;
+ source->tok.type = primitive(TUNDEFINED);
source->tok.u.u = 0;
source->tok.lhs = NULL;
source->tok.rhs = NULL;
@@ -1865,7 +1867,7 @@ makedecl(Source *source, int key, DeclKind kind)
* getloc() up in the source-code. */
decl->loc = source->tok.loc;
decl->key = key;
- decl->type = prim + TVOID;
+ decl->type = primitive(TVOID);
decl->contentenv = NULL;
appenddecltoenv(decl, currenv);
@@ -1884,7 +1886,7 @@ defertypedeclaration(Source *source, int key)
/* FIXME(m21c): type may be overwritten, when the declaration
* is completed */
- decl->type = maketype(&source->tok.loc, prim + TVOID, NULL);
+ decl->type = maketype(&source->tok.loc, primitive(TVOID), NULL);
decl->type->module = decl;
source->currenv = savedcurrenv;
@@ -2091,7 +2093,7 @@ advance:
flags = qualifiers(source, QTYPE);
if (getkind(source) == LSQRDELIM) {
- Type *tmp = maketype(getloc(source), prim + TARRAY, basetype);
+ Type *tmp = maketype(getloc(source), primitive(TARRAY), basetype);
basetype = tmp;
gettok(source);
@@ -2103,7 +2105,7 @@ advance:
}
if (getkind(source) == OMUL) {
- Type *tmp = maketype(getloc(source), prim + TPTR, basetype);
+ Type *tmp = maketype(getloc(source), primitive(TPTR), basetype);
basetype = tmp;
gettok(source);
@@ -2265,7 +2267,7 @@ redodeclaration:
"cannot infer return type of function");
}
- decl->type = maketype(&decl->loc, prim + TFUNCTION, paramtype);
+ decl->type = maketype(&decl->loc, primitive(TFUNCTION), paramtype);
decl->type->u.rtarget = ty;
ty = decl->type;
@@ -2376,7 +2378,7 @@ readident(Source *source, int flags)
lhs->u.u = 0;
}
- lhs->type = prim + TVOID;
+ lhs->type = primitive(TVOID);
}
if (flags & QCONST) {
@@ -2513,7 +2515,7 @@ readatom(Source *source, int flags)
lhs->lhs->kind == TYPE &&
lhs->rhs->kind == TYPE)
{
- Type *ty = maketype(&lhs->loc, prim + TTUPLE, NULL);
+ Type *ty = maketype(&lhs->loc, primitive(TTUPLE), NULL);
ty->target = lhs->lhs->type;
ty->u.rtarget = lhs->rhs->type;
deletenode(lhs);
@@ -2560,14 +2562,14 @@ readatom(Source *source, int flags)
case KVAR:
gettok(source);
- lhs = declaration(source, prim + TINFER, false);
+ lhs = declaration(source, primitive(TINFER), false);
break;
case KFALSE:
case KTRUE:
lhs = tokennode(source, NULL);
lhs->kind = NUMBER;
- lhs->type = prim + TBOOL;
+ lhs->type = primitive(TBOOL);
lhs->u.u = (uint64_t) (getkind(source) == KTRUE);
gettok(source);
break;
@@ -2575,7 +2577,7 @@ readatom(Source *source, int flags)
case KNULL:
lhs = tokennode(source, NULL);
lhs->kind = NUMBER;
- lhs->type = maketype(&source->tok.loc, prim + TPTR, prim + TVOID);
+ lhs->type = maketype(&source->tok.loc, primitive(TPTR), primitive(TVOID));
lhs->u.u = (uint64_t) (getkind(source) == KTRUE);
gettok(source);
break;
@@ -2712,7 +2714,7 @@ readatom(Source *source, int flags)
error(getloc(source), "expected expression");
lhs = tokennode(source, NULL);
lhs->kind = NUMBER;
- lhs->type = prim + TERRTYPE;
+ lhs->type = primitive(TERRTYPE);
lhs->u.u = 0;
gettok(source);
}
@@ -2887,7 +2889,7 @@ exprlist(Source *source, bool isparam, Type *paramtype)
lhs->lhs->kind == TYPE &&
lhs->rhs->kind == TYPE)
{
- lhs->type = maketype(&lhs->loc, prim + TTUPLE,
+ lhs->type = maketype(&lhs->loc, primitive(TTUPLE),
lhs->lhs->type);
lhs->type->u.rtarget = lhs->rhs->type;
@@ -3123,10 +3125,10 @@ conv(Node *node)
assert(ty);
if (ty->kind == TINFER)
- return wrap(prim + TINT, node);
+ return wrap(primitive(TINT), node);
if (ty->kind == TUINFER)
- return wrap(prim + TUINT, node);
+ return wrap(primitive(TUINT), node);
return node;
}