aboutsummaryrefslogtreecommitdiff
path: root/semantics.c
diff options
context:
space:
mode:
authorTeddy <[email protected]>2014-04-06 10:26:37 +0800
committerTeddy <[email protected]>2014-04-06 10:26:37 +0800
commitf446f65340d41d6b4558a26912ba15e28059fe48 (patch)
tree8d1c1208787851dc92761422b4e360d35f98c885 /semantics.c
parent7384b7e2cbe9f40970c74bc5afe589c3df4fe24d (diff)
complex cast almost donecomplex_cast
Diffstat (limited to 'semantics.c')
-rw-r--r--semantics.c47
1 files changed, 21 insertions, 26 deletions
diff --git a/semantics.c b/semantics.c
index 68884ce..39ca0c1 100644
--- a/semantics.c
+++ b/semantics.c
@@ -493,7 +493,8 @@ CVar_t semantics_declr(CNode *p, CType_t type_spec, CScope_t scope, int func_chk
if (!func_chk) CHECK_CVOID(p->rec.strval, p);
return cvar_create(p->rec.strval, type_spec, p);
}
-
+ if (p->type == NOP) /* type name */
+ return cvar_create(NULL, type_spec, p);
switch (p->rec.subtype)
{
case DECLR_FUNC:
@@ -681,37 +682,30 @@ ExpType exp_check_aseq(ExpType lhs, ExpType rhs, CNode *ast) {
#define IS_INT(tt) ((tt) == CINT || (tt) == CCHAR)
#define IS_ARITH(tt) IS_INT(tt)
-#define IS_SCALAR(tt) (IS_ARITH(tt) || (tt) == CPTR || (tt) == CARR)
+#define IS_SCALAR(tt) (!((tt) == CUNION || (tt) == CSTRUCT))
-CType_t semantics_cast(CNode *p, CScope_t scope) {
- CNode *t, *ast;
- CType_t tt, type;
- if (p->type == TYPE_SPEC)
+CType_t semantics_typename(CNode *p, CScope_t scope) {
+ CVar_t var = semantics_declr(p->chd->next,
+ semantics_type_spec(p->chd, scope),
+ scope, 0);
+ CType_t type = var->type;
+ free(var);
+ if (!IS_SCALAR(type->type))
{
- type = semantics_type_spec(p, scope);
- ast = p;
+ sprintf(err_buff, "conversion to non-scalar type requested");
+ ERROR(p);
}
- else
+ if (type->type == CARR)
{
- type = ctype_create("", CPTR, p); /* pointer */
- for (t = p, tt = type;; t = t->chd)
- {
- if (t->chd->type == TYPE_SPEC)
- {
- tt->rec.ref = semantics_type_spec(t->chd, scope);
- ast = t;
- break;
- }
- tt->rec.ref = ctype_create("", CPTR, t);
- tt = tt->rec.ref;
- }
+ sprintf(err_buff, "cast specifies array type");
+ ERROR(p);
}
- if (!IS_SCALAR(type->type))
+ if (type->type == CFUNC)
{
- sprintf(err_buff, "conversion to non-scalar type requested");
- ERROR(ast);
+ sprintf(err_buff, "cast specifies function type");
+ ERROR(p);
}
- type->ast = ast;
+ type->ast = p;
return type;
}
@@ -1121,7 +1115,7 @@ ExpType semantics_exp(CNode *p, CScope_t scope) {
res = exp_check_arith(op1, op2, p);
break;
case EXP_CAST:
- res.type = semantics_cast(p->chd, scope);
+ res.type = semantics_typename(p->chd, scope);
res.lval = 0;
break;
case '&':
@@ -1395,6 +1389,7 @@ CVar_t semantics_func(CNode *p, CScope_t scope) {
funco->rec.func.body = res->type->rec.func.body;
free(res);
}
+ free(head);
return old;
}