F diff --git a/misc/print.c b/misc/print.c
--- a/misc/print.c
+++ b/misc/print.c
void print_type(FILE *out,struct Type *type,char should_print_struct_union)
{
+ print_type_qualifier(out,type);
switch(type->specifier)
{
case TS_VOID:
print_denoted(out,(struct Denoted*)func->arguments[i]);
}
}
+ void print_type_qualifier(FILE *out,struct Type *type)
+ {
+
+ switch(type->specifier)
+ {
+ case TS_VOID:
+ case TS_CHAR:
+ case TS_INT:
+ case TS_FLOAT:
+ case TS_DOUBLE:
+ fprintf(out,"%s %s",
+ (AS_BASIC_TYPE_PTR(type)->is_const?"constant ":""),
+ (AS_BASIC_TYPE_PTR(type)->is_volatile ?"volatile ":"")
+ );
+ break;
+ case TS_STRUCT:
+ case TS_UNION:
+ fprintf(out,"%s %s",
+ (AS_STRUCT_UNION_PTR(type)->is_const?"constant ":""),
+ (AS_STRUCT_UNION_PTR(type)->is_volatile ?"volatile ":"")
+ );
+ break;
+ case TS_ENUM:
+ fprintf(out,"%s %s",
+ (AS_STRUCT_UNION_PTR(type)->is_const?"constant ":""),
+ (AS_STRUCT_UNION_PTR(type)->is_volatile ?"volatile ":"")
+ );
+ break;
+ case TS_POINTER:
+ fprintf(out,"%s %s",
+ (AS_TYPE_PTR_PTR(type)->is_const?"constant ":""),
+ (AS_TYPE_PTR_PTR(type)->is_volatile ?"volatile ":"")
+ );
+ break;
+ case TS_ARRAY:
+ case TS_FUNC:
+ case TS_BITFIELD:
+ break;
+ case TS_NONE:
+ case TS_ERROR:
+ default:
+ fprintf(out,"error");
+ }
+ }
#undef TOK
#undef INDENT
F diff --git a/misc/print.h b/misc/print.h
--- a/misc/print.h
+++ b/misc/print.h
void print_function_definition(FILE *out,struct Denoted_Function *function);
void print_errors(FILE *out,struct Queue *errors);
void print_function_args(FILE *out,struct Type_Function *func);
+ void print_type_qualifier(FILE *out,struct Type *type);
#endif
F diff --git a/parse/parse_declaration.c b/parse/parse_declaration.c
--- a/parse/parse_declaration.c
+++ b/parse/parse_declaration.c
((struct Denoted_Function*)hold)->body=(struct AST_Compound_Statement*)parse_finish_compound_statement(translation_data,scope);
Queue_Push(where_to_push,get_function_definition_tree(scope,(struct Denoted_Function*)hold));
- Scope_Push(scope,hold);
+ Scope_Push(scope,hold,translation_data);
free(prototype);
return;
}
return;
}
- Scope_Push(scope,hold);
+ Scope_Push(scope,hold,translation_data);
parse_function_definitions=0;
if(!get_and_check(translation_data,KW_COMMA))
{
{
struct Struct_Union *body;
body=get_struct_union_base(scope,ret->specifier);
- Scope_Push(scope,get_denoted_struct_union(id,body));
+ Scope_Push(scope,get_denoted_struct_union(id,body),translation_data);
parse_struct_union_specifier_finish(translation_data,scope,body);
ret->struct_union=body;
{
struct Enum *body;
body=get_enum_base();
- Scope_Push(scope,get_denoted_enum(id,body));
+ Scope_Push(scope,get_denoted_enum(id,body),translation_data);
parse_enum_specifier_finish(translation_data,scope,body);
ret->enumerator=body;
}else
void parse_declarator_inner(struct Translation_Data *translation_data,struct Scope *scope,struct Denoted_Base *base)
{
enum KEYWORDS hold;
+ char is_const;
+ char is_volatile;
while(get_and_check(translation_data,KW_STAR))
{
- base->type=(struct Type*)get_pointer_type(base->type);
+ is_const=is_volatile=0;
while(1)
{
hold=kw_get(translation_data);
if(hold==KW_CONST)
{
- ((struct Type_Pointer*)base)->is_const=1;
+ chomp(translation_data);
+ is_const=1;
}else if(hold==KW_VOLATILE)
{
- ((struct Type_Pointer*)base)->is_volatile=1;
+ chomp(translation_data);
+ is_volatile=1;
}else
{
break;
}
}
+ base->type=(struct Type*)get_pointer_type(base->type,is_const,is_volatile);
}
parse_direct_declarator(translation_data,scope,base);
*/
void parse_direct_declarator_finish(struct Translation_Data *translation_data,struct Scope *scope,struct Denoted_Base *base)
{
+ struct AST *hold_expression;
while(1)
{
if(get_and_check(translation_data,KW_OPEN_SQUARE))
{
if(get_and_check(translation_data,KW_CLOSE_SQUARE))
{
- base->type=(struct Type*)get_array_type(base->type,NULL);
+ hold_expression=NULL;
}else
{
- base->type=(struct Type*)get_array_type(base->type,parse_expression(translation_data,scope));
+ hold_expression=parse_expression(translation_data,scope);
if(!get_and_check(translation_data,KW_CLOSE_SQUARE))
{
/*TODO error*/
push_translation_error("']' expected",translation_data);
base->type=(struct Type*)get_type_error(base->type);
+ delete_ast(hold_expression);
return;
}
}
+
+ parse_direct_declarator_finish(translation_data,scope,base);
+ base->type=get_array_type(base->type,hold_expression);
+
}else if(get_and_check(translation_data,KW_OPEN_NORMAL))
{
struct Queue *parameters;
hold=parse_struct_declarator(translation_data,struct_scope,prototype);
if(hold!=NULL && hold->denotation!=DT_Error)
{
- Scope_Push(struct_scope,hold);
+ Scope_Push(struct_scope,hold,translation_data);
Queue_Push(members,hold);
}else
}else
{
hold=parse_declarator(translation_data,scope,prototype);
- if(!get_and_check(translation_data,KW_COLUMN))
+ if(get_and_check(translation_data,KW_COLUMN))
{
- push_translation_error("column expected in struct declaration",translation_data);
- return get_denoted_error(hold);
+ /*TODO move error detection in get_type_bitfield*/
+ ((struct Denoted_Object*)hold)->object->type=(struct Type*)get_type_bitfield(prototype->type,parse_expression(translation_data,scope));
}
}
- /*TODO move error detection in get_type_bitfield*/
- ((struct Denoted_Object*)hold)->object->type=(struct Type*)get_type_bitfield(prototype->type,parse_expression(translation_data,scope));
return hold;
}
/*
hold=extract_denoted(base,prototype,1);
if(((struct Denoted_Object*)hold)->id!=NULL)
- Scope_Push((struct Scope*)function_prototype_scope,hold);
+ Scope_Push((struct Scope*)function_prototype_scope,hold,translation_data);
Queue_Push(parameters,((struct Denoted_Object*)hold));
delete_denoted_prototype(prototype);
F diff --git a/semantics/denoted.hh b/semantics/denoted.hh
--- a/semantics/denoted.hh
+++ b/semantics/denoted.hh
#ifndef GCC_DENOTED_HH
#define GCC_DENOTED_HH GCC_DENOTED_HH
+ #define AS_DENOTED_OBJECT_PTR(x) ((struct Denoted_Object*)x)
+ #define AS_DENOTED_FUNCTION(x) ((struct Denoted_Object*)x)
+ #define AS_DENOTED_TYPEDEF(x) ((struct Denoted_Object*)x)
+ #define AS_DENOTED_ENUM(x) ((struct Denoted_Object*)x)
+ #define AS_DENOTED_ENUM_CONST(x) ((struct Denoted_Object*)x)
+ #define AS_DENOTED_STRUCT_UNION(x) ((struct denoted_object*)x)
enum Denotation_Type
{
F diff --git a/semantics/scope.c b/semantics/scope.c
--- a/semantics/scope.c
+++ b/semantics/scope.c
return hold;
}
- void Scope_Push(struct Scope *scope,struct Denoted *declarator)
+ void Scope_Push(struct Scope *scope,struct Denoted *declarator,struct Translation_Data *translation_data)
{
+ /*TODO remove this macro */
+ #define CHECK_IF_ID_IS_TAKEN_THEN_PUSH(type,namespace) \
+ if(check_##namespace(scope,((type*)declarator)->id))\
+ { push_translation_error("redeclaration of id",translation_data);delete_denoted(declarator); return;}\
+ else { push_##namespace(scope,((type*)declarator)->id,declarator); }
+
+
switch(declarator->denotation)
{
- /*
case DT_Label:
- if(check_label(scope,((struct Denoted_Object
- return 1;
- */
+ /*perhaps lables should be denoted*/
+ assert(0);
case DT_Function:
- if(scope->type==FILE_SCOPE)
- {
- ((struct Denoted_Function*)declarator)->storage_class=SC_EXTERN;
- }
- switch(((struct Denoted_Function*)declarator)->storage_class)
- {
- case SC_EXTERN:
- while(scope->type!=EXTERN_SCOPE)
- scope=scope->parent;
- break;
- case SC_STATIC:
- assert(scope->type!=EXTERN_SCOPE);
-
- while(scope->type!=FILE_SCOPE)
- scope=scope->parent;
- break;
- }
- goto hack;
+ CHECK_IF_ID_IS_TAKEN_THEN_PUSH(struct Denoted_Function,ordinary);
+ break;
case DT_Object:
- switch(((struct Denoted_Object*)declarator)->object->storage_class)
- {
- case SC_EXTERN:
- while(scope->type!=EXTERN_SCOPE)
- scope=scope->parent;
- break;
- case SC_STATIC:
- assert(scope->type!=EXTERN_SCOPE);
-
- while(scope->type!=FILE_SCOPE)
- scope=scope->parent;
- break;
- }
- goto hack;
+ CHECK_IF_ID_IS_TAKEN_THEN_PUSH(struct Denoted_Object,ordinary);
+ break;
case DT_Typedef:
+ CHECK_IF_ID_IS_TAKEN_THEN_PUSH(struct Denoted_Typedef,ordinary);
+ break;
case DT_Enum_Constant:
+ CHECK_IF_ID_IS_TAKEN_THEN_PUSH(struct Denoted_Enum_Const,ordinary);
+ break;
case DT_Struct_Union_Member:
- hack:
- push_ordinary(scope,((struct Denoted_Object*)declarator)->id,declarator);
- return;
+ CHECK_IF_ID_IS_TAKEN_THEN_PUSH(struct Denoted_Object,ordinary);
+ break;
case DT_Enum:
+ CHECK_IF_ID_IS_TAKEN_THEN_PUSH(struct Denoted_Enum,tag);
+ break;
case DT_Struct_Union_Tag:
- push_tag(scope,((struct Denoted_Object*)declarator)->id,declarator);
- return;
+ CHECK_IF_ID_IS_TAKEN_THEN_PUSH(struct Denoted_Struct_Union,tag);
+ break;
}
+
+
+ #undef CHECK_IF_ID_IS_TAKEN_THEN_PUSH
}
char check_if_typedefed(struct Scope* scope,struct token *id)
{
F diff --git a/semantics/scope.h b/semantics/scope.h
--- a/semantics/scope.h
+++ b/semantics/scope.h
void* check_ordinary(struct Scope *current,struct token *id);
void push_ordinary(struct Scope *current,struct token *id,struct Denoted *denot);
- void Scope_Push(struct Scope *scope,struct Denoted *declarator);
+ void Scope_Push(struct Scope *scope,struct Denoted *declarator,struct Translation_Data *translation_data);
char check_if_typedefed(struct Scope* scope,struct token *id);
F diff --git a/semantics/type.c b/semantics/type.c
--- a/semantics/type.c
+++ b/semantics/type.c
return (struct Type*)ret;
}
- struct Type* get_pointer_type(struct Type *points_to)
+ struct Type* get_pointer_type(struct Type *points_to,char is_const,char is_volatile)
{
struct Type_Pointer *ret;
ret=calloc(1,sizeof(struct Type_Pointer));
ret->specifier=TS_POINTER;
ret->size=PTR_SIZE;
ret->points_to=points_to;
- ret->is_const=0;
- ret->is_volatile=0;
+ ret->is_const=is_const;
+ ret->is_volatile=is_volatile;
ret=(struct Type_Pointer*)type_check_and_push((struct Type*)ret,points_to->node,sizeof(struct Type_Pointer));
return (struct Type*)ret;
F diff --git a/semantics/type.h b/semantics/type.h
--- a/semantics/type.h
+++ b/semantics/type.h
#include <map.h>
#include <ast.h>
- #define PTR_SIZE 4
-
- #define INT_SIZE 4
-
- #define CHAR_SIZE 1
-
- #define FLOAT_SIZE 4
- #define DOUBLE_SIZE 8
struct Struct_Union* get_struct_union_base(struct Scope *scope ,enum Type_Specifier struct_or_union);
struct Enum *get_enum_base();
struct Type* get_basic_type(struct Denotation_Prototype *prototype);
- struct Type* get_pointer_type(struct Type *points_to);
+ struct Type* get_pointer_type(struct Type *points_to,char is_const,char is_volatile);
struct Type* get_array_type(struct Type *array_of,struct AST* number_of_elements);
struct Type* get_enum_type(struct Denotation_Prototype *prototype);
struct Type* get_type_bitfield(struct Type *base,struct AST* number_of_bits);
F diff --git a/semantics/type.hh b/semantics/type.hh
--- a/semantics/type.hh
+++ b/semantics/type.hh
#ifndef GCC_TYPE_HH
#define GCC_TYPE_HH GCC_TYPE_HH
+ #define PTR_SIZE 4
+ #define INT_SIZE 4
+ #define CHAR_SIZE 1
+ #define FLOAT_SIZE 4
+ #define DOUBLE_SIZE 8
+ #define AS_BASIC_TYPE_PTR(x) ((struct Type_Basic*)x)
+ #define AS_STRUCT_UNION_PTR(x) ((struct Type_Struct_Union*)x)
+ #define AS_TYPE_PTR_PTR(x) ((struct Type_Pointer*)x)
+ #define AS_TYPE_ARR_PTR(x) ((struct Type_Array*)x)
+ #define AS_TYPE_ENUM_PTR(x) ((struct Type_Enum*)x)
/*this isn't just type-specifier :DD*/
enum Type_Specifier
F diff --git a/tests/test.c b/tests/test.c
--- a/tests/test.c
+++ b/tests/test.c
- /*asdf*/#include/*asdf*/"test2.c"
- #define max(x,y) (x>y?x:y)
-
-
- struct asdf
+ int main()
{
- List kek;
- }a;
- int main(int argc, char *argv[])
- {
- int fw;
- int fx;
- int fy;
- int fz;
- int ga;
- int gb;
- int gc;
- int gd;
- int ge;
- int gf;
- int gg;
- int gh;
- int gi;
- int gj;
- int gk;
- int gl;
- int gm;
- int gn;
- int go;
- int gp;
- int gq;
- int gr;
- int gs;
- int gt;
- int gu;
- int gv;
- int gw;
- int gx;
- int gy;
- int gz;
- int ha;
- int hb;
- int hc;
- int hd;
- int he;
- int hf;
- int hg;
- int hh;
- int hi;
- int hj;
- int hk;
- int hl;
- int hm;
- int hn;
- int ho;
- int hp;
- int hq;
- int hr;
- int hs;
- int ht;
- int hu;
- int hv;
- int hw;
- int hx;
- int hy;
- int hz;
- int ia;
- int ib;
- int ic;
- int id;
- int ie;
- int ig;
- int ih;
- int ii;
- int ij;
- int ik;
- int il;
- int im;
- int in;
- int io;
- int ip;
- int iq;
- int ir;
- int is;
- int it;
- int iu;
- int iv;
- int iw;
- int ix;
- int iy;
- int iz;
- int ja;
- int jb;
- int jc;
- int jd;
- int je;
- int jf;
- int jg;
- int jh;
- int ji;
- int jj;
- int jk;
- int jl;
- int jm;
- int jn;
- int jo;
- int jp;
- int jq;
- int jr;
- int js;
- int jt;
- int ju;
- int jv;
- int jw;
- int jx;
- int jy;
- int jz;
- int ka;
- int kb;
- int kc;
- int kd;
- int ke;
- int kf;
- int kg;
- int kh;
- int ki;
- int kj;
- int kk;
- int kl;
- int km;
- int kn;
- int ko;
- int kp;
- int kq;
- int kr;
- int ks;
- int kt;
- int ku;
- int kv;
- int kw;
- int kx;
- int ky;
- int kz;
- int la;
- int lb;
- int lc;
- int ld;
- int le;
- int lf;
- int lg;
- int lh;
- int li;
- int lj;
- int lk;
- int ll;
- int lm;
- int ln;
- int lo;
- int lp;
- int lq;
- int lr;
- int ls;
- int lt;
- int lu;
- int lv;
- int lw;
- int lx;
- int ly;
- int lz;
- int ma;
- int mb;
- int mc;
- int md;
- int me;
- int mf;
- int mg;
- int mh;
- int mi;
- int mj;
- int mk;
- int ml;
- int mm;
- int mn;
- int mo;
- int mp;
- int mq;
- int mr;
- int ms;
- int mt;
- int mu;
- int mv;
- int mw;
- int mx;
- int my;
- int mz;
- int na;
- int nb;
- int nc;
- int nd;
- int ne;
- int nf;
- int ng;
- int nh;
- int ni;
- int nj;
- int nk;
- int nl;
- int nm;
- int nn;
- int no;
- int np;
- int nq;
- int nr;
- int ns;
- int nt;
- int nu;
- int nv;
- int nw;
- int nx;
- int ny;
- int nz;
- int oa;
- int ob;
- int oc;
- int od;
- int oe;
- int of;
- int og;
- int oh;
- int oi;
- int oj;
- int ok;
- int ol;
- int om;
- int on;
- int oo;
- int op;
- int oq;
- int or;
- int os;
- int ot;
- int ou;
- int ov;
- int ow;
- int ox;
- int oy;
- int oz;
- int pa;
- int pb;
- int pc;
- int pd;
- int pe;
- int pf;
- int pg;
- int ph;
- int pi;
- int pj;
- int pk;
- int pl;
- int pm;
- int pn;
- int po;
- int pp;
- int pq;
- int pr;
- int ps;
- int pt;
- int pu;
- int pv;
- int pw;
- int px;
- int py;
- int pz;
- int qa;
- int qb;
- int qc;
- int qd;
- int qe;
- int qf;
- int qg;
- int qh;
- int qi;
- int qj;
- int qk;
- int ql;
- int qm;
- int qn;
- int qo;
- int qp;
- int qq;
- int qr;
- int qs;
- int qt;
- int qu;
- int qv;
- int qw;
- int qx;
- int qy;
- int qz;
- int ra;
- int rb;
- int rc;
- int rd;
- int re;
- int rf;
- int rg;
- int rh;
- int ri;
- int rj;
- int rk;
- int rl;
- int rm;
- int rn;
- int ro;
- int rp;
- int rq;
- int rr;
- int rs;
- int rt;
- int ru;
- int rv;
- int rw;
- int rx;
- int ry;
- int rz;
- int sa;
- int sb;
- int sc;
- int sd;
- int se;
- int sf;
- int sg;
- int sh;
- int si;
- int sj;
- int sk;
- int sl;
- int sm;
- int sn;
- int so;
- int sp;
- int sq;
- int sr;
- int ss;
- int st;
- int su;
- int sv;
- int sw;
- int sx;
- int sy;
- int sz;
- int ta;
- int tb;
- int tc;
- int td;
- int te;
- int tf;
- int tg;
- int th;
- int ti;
- int tj;
- int tk;
- int tl;
- int tm;
- int tn;
- int to;
- int tp;
- int tq;
- int tr;
- int ts;
- int tt;
- int tu;
- int tv;
- int tw;
- int tx;
- int ty;
- int tz;
- int ua;
- int ub;
- int uc;
- int ud;
- int ue;
- int uf;
- int ug;
- int uh;
- int ui;
- int uj;
- int uk;
- int ul;
- int um;
- int un;
- int uo;
- int up;
- int uq;
- int ur;
- int us;
- int ut;
- int uu;
- int uv;
- int uw;
- int ux;
- int uy;
- int uz;
- int va;
- int vb;
- int vc;
- int vd;
- int ve;
- int vf;
- int vg;
- int vh;
- int vi;
- int vj;
- int vk;
- int vl;
- int vm;
- int vn;
- int vo;
- int vp;
- int vq;
- int vr;
- int vs;
- int vt;
- int vu;
- int vv;
- int vw;
- int vx;
- int vy;
- int vz;
- int wa;
- int wb;
- int wc;
- int wd;
- int we;
- int wf;
- int wg;
- int wh;
- int wi;
- int wj;
- int wk;
- int wl;
- int wm;
- int wn;
- int wo;
- int wp;
- int wq;
- int wr;
- int ws;
- int wt;
- int wu;
- int wv;
- int ww;
- int wx;
- int wy;
- int wz;
- int xa;
- int xb;
- int xc;
- int xd;
- int xe;
- int xf;
- int xg;
- int xh;
- int xi;
- int xj;
- int xk;
- int xl;
- int xm;
- int xn;
- int xo;
- int xp;
- int xq;
- int xr;
- int xs;
- int xt;
- int xu;
- int xv;
- int xw;
- int xx;
- int xy;
- int xz;
- int ya;
- int yb;
- int yc;
- int yd;
- int ye;
- int yf;
- int yg;
- int yh;
- int yi;
- int yj;
- int yk;
- int yl;
- int ym;
- int yn;
- int yo;
- int yp;
- int yq;
- int yr;
- int ys;
- int yt;
- int yu;
- int yv;
- int yw;
- int yx;
- int yy;
- int yz;
- int za;
- int zb;
- int zc;
- int zd;
- int ze;
- int zf;
- int zg;
- int zh;
- int zi;
- int zj;
- int zk;
- int zl;
- int zm;
- int zn;
- int zo;
- int zp;
- int zq;
- int zr;
- int zs;
- int zt;
- int zu;
- int zv;
- int zw;
- int zx;
- int zy;
- int zz=1;
+ int A[2][3][4];
+ const int * const * volatile a;
+ const int * const * volatile b;
+ struct a;
+ struct a c;
+ return 1+1;
}
F diff --git a/tests/test3.c b/tests/test3.c
--- a/tests/test3.c
+++ b/tests/test3.c
struct A
{
- int c:123;
+ int a:1;
}a;
- extern int external_int;
- static int static_int;
- static int fib(int n)
+ int external_int;
+ int static_int;
+ int fib(int n)
{
int a=1,b=1,c;
for(n;n>0;--n)
return a;
}
- extern void alert(int a);
- extern int main()
+ int main()
{
- int k[10*10][3][5];
+ int *a;
external_int++;
alert(fib(10));
return 0;