1
0
mirror of https://github.com/golang/go synced 2024-11-26 01:17:57 -07:00

Implement unsafe.Alignof.

R=ken
DELTA=20  (19 added, 0 deleted, 1 changed)
OCL=24719
CL=24771
This commit is contained in:
Ian Lance Taylor 2009-02-10 11:46:26 -08:00
parent d94c5aba12
commit 651972b31f
3 changed files with 20 additions and 1 deletions

View File

@ -1495,6 +1495,7 @@ unsafenmagic(Node *l, Node *r)
{
Node *n;
Sym *s;
Type *t;
long v;
Val val;
@ -1519,7 +1520,23 @@ unsafenmagic(Node *l, Node *r)
if(r->op != ODOT && r->op != ODOTPTR)
goto no;
walktype(r, Erv);
v = n->xoffset;
v = r->xoffset;
goto yes;
}
if(strcmp(s->name, "Alignof") == 0) {
walktype(r, Erv);
if (r->type == T)
goto no;
// make struct { byte; T; }
t = typ(TSTRUCT);
t->type = typ(TFIELD);
t->type->type = types[TUINT8];
t->type->down = typ(TFIELD);
t->type->down->type = r->type;
// compute struct widths
dowidth(t);
// the offset of T is its required alignment
v = t->type->down->width;
goto yes;
}

View File

@ -67,5 +67,6 @@ char *unsafeimport =
"type unsafe.Pointer *any\n"
"func unsafe.Offsetof (? any) (? int)\n"
"func unsafe.Sizeof (? any) (? int)\n"
"func unsafe.Alignof (? any) (? int)\n"
"\n"
"$$\n";

View File

@ -8,3 +8,4 @@ package PACKAGE
type Pointer *any;
func Offsetof(any) int;
func Sizeof(any) int;
func Alignof(any) int;