1
0
mirror of https://github.com/golang/go synced 2024-11-23 09:30:03 -07:00

embedded methods

R=r
OCL=17851
CL=17851
This commit is contained in:
Ken Thompson 2008-10-25 13:31:25 -07:00
parent 22a6747999
commit b642cf8c00
3 changed files with 71 additions and 30 deletions

View File

@ -513,7 +513,9 @@ void
gentramp(Type *t, Sig *b) gentramp(Type *t, Sig *b)
{ {
Sym *e; Sym *e;
int c, d; int c, d, o;
Prog *p;
Type *f;
e = lookup(b->name); e = lookup(b->name);
for(d=0; d<nelem(dotlist); d++) { for(d=0; d<nelem(dotlist); d++) {
@ -524,25 +526,74 @@ gentramp(Type *t, Sig *b)
fatal("gentramp"); fatal("gentramp");
out: out:
print("gentramp %d\n", d); // print("gentramp %d\n", d);
print(" t = %lT\n", t); // print(" t = %lT\n", t);
print(" name = %s\n", b->name); // print(" name = %s\n", b->name);
print(" sym = %S\n", b->sym); // print(" sym = %S\n", b->sym);
print(" hash = 0x%ux\n", b->hash); // print(" hash = 0x%ux\n", b->hash);
//TEXT main·S_test2(SB),7,$0
p = pc;
gins(ATEXT, N, N);
p->from.type = D_EXTERN;
p->from.sym = b->sym;
p->to.type = D_CONST;
p->to.offset = 0;
p->from.scale = 7;
//print("1. %P\n", p);
//MOVQ 8(SP), AX
p = pc;
gins(AMOVQ, N, N);
p->from.type = D_INDIR+D_SP;
p->from.offset = 8;
p->to.type = D_AX;
//print("2. %P\n", p);
o = 0;
for(c=d-1; c>=0; c--) { for(c=d-1; c>=0; c--) {
print(" %d %d %S\n", f = dotlist[c].field;
dotlist[c].ptr, o += f->width;
dotlist[c].offset, if(!isptr[f->type->etype])
dotlist[c].sym); continue;
//MOVQ o(AX), AX
p = pc;
gins(AMOVQ, N, N);
p->from.type = D_INDIR+D_AX;
p->from.offset = o;
p->to.type = D_AX;
//print("3. %P\n", p);
o = 0;
}
if(o != 0) {
//ADDQ $XX, AX
p = pc;
gins(AADDQ, N, N);
p->from.type = D_CONST;
p->from.offset = o;
p->to.type = D_AX;
//print("4. %P\n", p);
} }
//TEXT main·S_test2(SB),7,$0 //MOVQ AX, 8(SP)
// MOVQ 8(SP), AX p = pc;
// MOVQ XX(AX), AX gins(AMOVQ, N, N);
// ADDQ $XX, AX p->from.type = D_AX;
// MOVQ AX, 8(SP) p->to.type = D_INDIR+D_SP;
// JMP main·Sub_test2(SB) p->to.offset = 8;
//print("5. %P\n", p);
f = dotlist[0].field;
//JMP main·Sub_test2(SB)
snprint(namebuf, sizeof(namebuf), "%s_%s",
f->sym->name, b->name);
if(isptr[f->type->etype])
f = f->type;
p = pc;
gins(AJMP, N, N);
p->to.type = D_EXTERN;
p->to.sym = pkglookup(namebuf, f->type->sym->opackage);
//print("6. %P\n", p);
} }
void void

View File

@ -402,9 +402,7 @@ struct Io
typedef struct Dlist Dlist; typedef struct Dlist Dlist;
struct Dlist struct Dlist
{ {
Sym* sym; Type* field;
uchar ptr;
int offset;
}; };
EXTERN Dlist dotlist[10]; // size is max depth of embeddeds EXTERN Dlist dotlist[10]; // size is max depth of embeddeds

View File

@ -2445,13 +2445,8 @@ adddot1(Sym *s, Type *t, int d)
if(f->sym == S) if(f->sym == S)
continue; continue;
a = adddot1(s, f->type, d); a = adddot1(s, f->type, d);
if(a != 0 && c == 0) { if(a != 0 && c == 0)
dotlist[d].sym = f->sym; dotlist[d].field = f;
dotlist[d].offset = f->width;
dotlist[d].ptr = 0;
if(isptr[f->type->etype])
dotlist[d].ptr = 1;
}
c += a; c += a;
} }
@ -2497,7 +2492,7 @@ out:
// rebuild elided dots // rebuild elided dots
for(c=d-1; c>=0; c--) { for(c=d-1; c>=0; c--) {
n = nod(ODOT, n, n->right); n = nod(ODOT, n, n->right);
n->left->right = newname(dotlist[c].sym); n->left->right = newname(dotlist[c].field->sym);
} }
return n; return n;
} }
@ -2609,7 +2604,6 @@ expandmeth(Sym *s, Type *t)
} }
} }
//print("expand %S: %lT", s, t);
for(sl=slist; sl!=nil; sl=sl->link) { for(sl=slist; sl!=nil; sl=sl->link) {
if(sl->good) { if(sl->good) {
// add it to the base type method list // add it to the base type method list
@ -2620,8 +2614,6 @@ expandmeth(Sym *s, Type *t)
f->down = t->method; f->down = t->method;
t->method = f; t->method = f;
//print(" %T", f);
} }
} }
//print("\n");
} }