Add typeof

This commit is contained in:
Rui Ueyama 2020-07-23 19:29:25 +09:00
parent 007e526ec5
commit 7d80a5136d
3 changed files with 37 additions and 3 deletions

26
parse.c
View File

@ -117,6 +117,7 @@ static bool is_typename(Token *tok);
static Type *declspec(Token **rest, Token *tok, VarAttr *attr);
static Type *typename(Token **rest, Token *tok);
static Type *enum_specifier(Token **rest, Token *tok);
static Type *typeof_specifier(Token **rest, Token *tok);
static Type *type_suffix(Token **rest, Token *tok, Type *ty);
static Type *declarator(Token **rest, Token *tok, Type *ty);
static Node *declaration(Token **rest, Token *tok, Type *basety, VarAttr *attr);
@ -365,7 +366,7 @@ static void push_tag_scope(Token *tok, Type *ty) {
// | "typedef" | "static" | "extern"
// | "signed" | "unsigned"
// | struct-decl | union-decl | typedef-name
// | enum-specifier
// | enum-specifier | typeof-specifier
// | "const" | "volatile" | "auto" | "register" | "restrict"
// | "__restrict" | "__restrict__" | "_Noreturn")+
//
@ -443,7 +444,8 @@ static Type *declspec(Token **rest, Token *tok, VarAttr *attr) {
// Handle user-defined types.
Type *ty2 = find_typedef(tok);
if (equal(tok, "struct") || equal(tok, "union") || equal(tok, "enum") || ty2) {
if (equal(tok, "struct") || equal(tok, "union") || equal(tok, "enum") ||
equal(tok, "typeof") || ty2) {
if (counter)
break;
@ -453,6 +455,8 @@ static Type *declspec(Token **rest, Token *tok, VarAttr *attr) {
ty = union_decl(&tok, tok->next);
} else if (equal(tok, "enum")) {
ty = enum_specifier(&tok, tok->next);
} else if (equal(tok, "typeof")) {
ty = typeof_specifier(&tok, tok->next);
} else {
ty = ty2;
tok = tok->next;
@ -764,6 +768,22 @@ static Type *enum_specifier(Token **rest, Token *tok) {
return ty;
}
// typeof-specifier = "(" (expr | typename) ")"
static Type *typeof_specifier(Token **rest, Token *tok) {
tok = skip(tok, "(");
Type *ty;
if (is_typename(tok)) {
ty = typename(&tok, tok);
} else {
Node *node = expr(&tok, tok);
add_type(node);
ty = node->ty;
}
*rest = skip(tok, ")");
return ty;
}
// declaration = declspec (declarator ("=" expr)? ("," declarator ("=" expr)?)*)? ";"
static Node *declaration(Token **rest, Token *tok, Type *basety, VarAttr *attr) {
Node head = {};
@ -1381,7 +1401,7 @@ static bool is_typename(Token *tok) {
"void", "_Bool", "char", "short", "int", "long", "struct", "union",
"typedef", "enum", "static", "extern", "_Alignas", "signed", "unsigned",
"const", "volatile", "auto", "register", "restrict", "__restrict",
"__restrict__", "_Noreturn", "float", "double",
"__restrict__", "_Noreturn", "float", "double", "typeof",
};
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)

13
test/typeof.c Normal file
View File

@ -0,0 +1,13 @@
#include "test.h"
int main() {
ASSERT(3, ({ typeof(int) x=3; x; }));
ASSERT(3, ({ typeof(1) x=3; x; }));
ASSERT(4, ({ int x; typeof(x) y; sizeof(y); }));
ASSERT(8, ({ int x; typeof(&x) y; sizeof(y); }));
ASSERT(4, ({ typeof("foo") x; sizeof(x); }));
ASSERT(12, sizeof(typeof(struct { int a,b,c; })));
printf("OK\n");
return 0;
}

View File

@ -163,6 +163,7 @@ static bool is_keyword(Token *tok) {
"default", "extern", "_Alignof", "_Alignas", "do", "signed",
"unsigned", "const", "volatile", "auto", "register", "restrict",
"__restrict", "__restrict__", "_Noreturn", "float", "double",
"typeof",
};
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)