Blob
1 #include <u.h>2 #include <libc.h>3 #include <bio.h>4 #include <ctype.h>5 #include <mach.h>6 #define Extern extern7 #include "acid.h"9 Type*10 srch(Type *t, char *s)11 {12 Type *f;14 f = 0;15 while(t) {16 if(strcmp(t->tag->name, s) == 0) {17 if(f == 0 || t->depth < f->depth)18 f = t;19 }20 t = t->next;21 }22 return f;23 }25 void26 odot(Node *n, Node *r)27 {28 char *s;29 Type *t;30 Node res;31 ulong addr;33 s = n->sym->name;34 if(s == 0)35 fatal("dodot: no tag");37 expr(n->left, &res);38 if(res.store.comt == 0)39 error("no type specified for (expr).%s", s);41 if(res.type != TINT)42 error("pointer must be integer for (expr).%s", s);44 t = srch(res.store.comt, s);45 if(t == 0)46 error("no tag for (expr).%s", s);48 /* Propagate types */49 if(t->type)50 r->store.comt = t->type->lt;52 addr = res.store.u.ival+t->offset;53 if(t->fmt == 'a') {54 r->op = OCONST;55 r->store.fmt = 'a';56 r->type = TINT;57 r->store.u.ival = addr;58 }59 else60 indir(cormap, addr, t->fmt, r);62 }64 static Type **tail;65 static Lsym *base;67 void68 buildtype(Node *m, int d)69 {70 Type *t;72 if(m == ZN)73 return;75 switch(m->op) {76 case OLIST:77 buildtype(m->left, d);78 buildtype(m->right, d);79 break;81 case OCTRUCT:82 buildtype(m->left, d+1);83 break;84 default:85 t = malloc(sizeof(Type));86 t->next = 0;87 t->depth = d;88 t->tag = m->sym;89 t->base = base;90 t->offset = m->store.u.ival;91 if(m->left) {92 t->type = m->left->sym;93 t->fmt = 'a';94 }95 else {96 t->type = 0;97 if(m->right)98 t->type = m->right->sym;99 t->fmt = m->store.fmt;100 }102 *tail = t;103 tail = &t->next;104 }105 }107 void108 defcomplex(Node *tn, Node *m)109 {110 tail = &tn->sym->lt;111 base = tn->sym;112 buildtype(m, 0);113 }115 void116 decl(Node *n)117 {118 Node *l;119 Value *v;120 Frtype *f;121 Lsym *type;123 type = n->sym;124 if(type->lt == 0)125 error("%s is not a complex type", type->name);127 l = n->left;128 if(l->op == ONAME) {129 v = l->sym->v;130 v->store.comt = type->lt;131 v->store.fmt = 'a';132 return;133 }135 /*136 * Frame declaration137 */138 for(f = l->sym->local; f; f = f->next) {139 if(f->var == l->left->sym) {140 f->type = n->sym->lt;141 return;142 }143 }144 f = malloc(sizeof(Frtype));145 if(f == 0)146 fatal("out of memory");148 f->type = type->lt;150 f->var = l->left->sym;151 f->next = l->sym->local;152 l->sym->local = f;153 }