mirror of https://github.com/rui314/chibicc
Add typeof
This commit is contained in:
parent
2a38d6471a
commit
bb38ad6a84
24
parse.c
24
parse.c
|
@ -123,6 +123,7 @@ static bool is_typename(Token *tok);
|
|||
static Type *typespec(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);
|
||||
|
@ -446,7 +447,8 @@ static Type *typespec(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;
|
||||
|
||||
|
@ -456,6 +458,8 @@ static Type *typespec(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 = typespec (declarator ("=" expr)? ("," declarator ("=" expr)?)*)? ";"
|
||||
static Node *declaration(Token **rest, Token *tok, Type *basety, VarAttr *attr) {
|
||||
Node head = {};
|
||||
|
@ -1376,7 +1396,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++)
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -150,6 +150,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++)
|
||||
|
|
Loading…
Reference in New Issue