]> Cypherpunks repositories - gostls13.git/commitdiff
gc: add error type
authorRuss Cox <rsc@golang.org>
Wed, 2 Nov 2011 01:46:41 +0000 (21:46 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 2 Nov 2011 01:46:41 +0000 (21:46 -0400)
R=ken
CC=golang-dev
https://golang.org/cl/5331043

src/cmd/gc/export.c
src/cmd/gc/fmt.c
src/cmd/gc/go.h
src/cmd/gc/lex.c
src/cmd/gc/reflect.c
src/cmd/gc/subr.c

index 06410a21431f8de9bf376291384b8d1164ee44f2..d0b28a25b32e9692e14fdbc476f1aa107b49fda9 100644 (file)
@@ -89,7 +89,6 @@ dumppkg(Pkg *p)
 }
 
 static void
-
 dumpexportconst(Sym *s)
 {
        Node *n;
@@ -151,7 +150,7 @@ dumpexporttype(Type *t)
        if(t == T)
                return;
 
-       if(t->printed || t == types[t->etype] || t == bytetype || t == runetype)
+       if(t->printed || t == types[t->etype] || t == bytetype || t == runetype || t == errortype)
                return;
        t->printed = 1;
 
index 4e57057a9345222edad56f3483692a177faf6e88..12ea3028270a97981d5e59e28c525665053821fc 100644 (file)
@@ -553,6 +553,9 @@ typefmt(Fmt *fp, Type *t)
                t = types[t->etype];
        }
 
+       if(t == errortype)
+               return fmtstrcpy(fp, "error");
+
        // Unless the 'l' flag was specified, if the type has a name, just print that name.
        if(!(fp->flags&FmtLong) && t->sym && t->etype != TFIELD && t != types[t->etype]) {
                switch(fmtmode) {
index 7557f74baa5ebc412345f8128738d0d789aa8ba8..7d6ac08433d2c9b8220268321667bf55112dfc0c 100644 (file)
@@ -786,6 +786,7 @@ EXTERN      Type*   idealstring;
 EXTERN Type*   idealbool;
 EXTERN Type*   bytetype;
 EXTERN Type*   runetype;
+EXTERN Type*   errortype;
 EXTERN uchar   simtype[NTYPE];
 EXTERN uchar   isptr[NTYPE];
 EXTERN uchar   isforw[NTYPE];
index a242b9b43cceea346d04fed62494c149b563a3a4..86492a53bcac90f7250ba02c8050fd8d6919747d 100644 (file)
@@ -1759,6 +1759,40 @@ static void
 lexinit1(void)
 {
        Sym *s, *s1;
+       Type *t, *f, *rcvr, *in, *out;
+
+       // t = interface { Error() string }
+       rcvr = typ(TSTRUCT);
+       rcvr->type = typ(TFIELD);
+       rcvr->type->type = ptrto(typ(TSTRUCT));
+       rcvr->funarg = 1;
+       in = typ(TSTRUCT);
+       in->funarg = 1;
+       out = typ(TSTRUCT);
+       out->type = typ(TFIELD);
+       out->type->type = types[TSTRING];
+       out->funarg = 1;
+       f = typ(TFUNC);
+       *getthis(f) = rcvr;
+       *getoutarg(f) = out;
+       *getinarg(f) = in;
+       f->thistuple = 1;
+       f->intuple = 0;
+       f->outnamed = 0;
+       f->outtuple = 1;
+       t = typ(TINTER);
+       t->type = typ(TFIELD);
+       t->type->sym = lookup("Error");
+       t->type->type = f;
+
+       // error type
+       s = lookup("error");
+       s->lexical = LNAME;
+       errortype = t;
+       errortype->sym = s;
+       s1 = pkglookup("error", builtinpkg);
+       s1->lexical = LNAME;
+       s1->def = typenod(errortype);
 
        // byte alias
        s = lookup("byte");
@@ -1820,6 +1854,10 @@ lexfini(void)
        s = lookup("byte");
        if(s->def == N)
                s->def = typenod(bytetype);
+       
+       s = lookup("error");
+       if(s->def == N)
+               s->def = typenod(errortype);
 
        s = lookup("rune");
        if(s->def == N)
index 3eefb0afe334b14983c8a47112401ad671c3df20..86df3a378d07534797f9d086b73e81ee61bf1b60 100644 (file)
@@ -693,8 +693,13 @@ dtypesym(Type *t)
                tbase = t->type;
        dupok = tbase->sym == S;
 
-       if(compiling_runtime && (tbase == types[tbase->etype] || tbase == bytetype || tbase == runetype))       // int, float, etc
+       if(compiling_runtime && 
+                       (tbase == types[tbase->etype] ||
+                       tbase == bytetype ||
+                       tbase == runetype ||
+                       tbase == errortype)) { // int, float, etc
                goto ok;
+       }
 
        // named types from other files are defined only by those files
        if(tbase->sym && !tbase->local)
@@ -903,6 +908,13 @@ dumptypestructs(void)
                        dtypesym(ptrto(types[i]));
                dtypesym(ptrto(types[TSTRING]));
                dtypesym(ptrto(types[TUNSAFEPTR]));
+
+               // emit type structs for error and func(error) string.
+               // The latter is the type of an auto-generated wrapper.
+               dtypesym(ptrto(errortype));
+               dtypesym(functype(nil, 
+                       list1(nod(ODCLFIELD, N, typenod(errortype))),
+                       list1(nod(ODCLFIELD, N, typenod(types[TSTRING])))));
                
                // add paths for runtime and main, which 6l imports implicitly.
                dimportpath(runtimepkg);
index 7843102abd473bba4f56802b6ce7e21a475cca68..dc1d3146381790790a813196e1f745093dfb43cc 100644 (file)
@@ -1483,7 +1483,7 @@ ptrto(Type *t)
        Type *t1;
 
        if(tptr == 0)
-               fatal("ptrto: nil");
+               fatal("ptrto: no tptr");
        t1 = typ(tptr);
        t1->type = t;
        t1->width = widthptr;