]> Cypherpunks repositories - gostls13.git/commitdiff
more import/export stuff:
authorRobert Griesemer <gri@golang.org>
Fri, 1 Aug 2008 20:33:31 +0000 (13:33 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 1 Aug 2008 20:33:31 +0000 (13:33 -0700)
- no need to import/export predeclared types
- fix for receiver types
- cleanups
- added tests to Makefile

R=r
OCL=13728
CL=13730

usr/gri/gosrc/export.go
usr/gri/gosrc/globals.go
usr/gri/gosrc/import.go
usr/gri/gosrc/parser.go
usr/gri/gosrc/universe.go

index 9d480fc50ce9d9b1d8421d6b946bf94c109a22c9..466296030e83ff51c307ca3b9c35ee5497a6a4e5 100755 (executable)
@@ -94,23 +94,23 @@ func (E *Exporter) WritePackageTag(tag int) {
                if tag >= 0 {
                        print " [P", tag, "]";  // package ref
                } else {
-                       print "\nP", E.pkg_ref, ": ", -tag;  // package no
+                       print "\nP", E.pkg_ref, ":";
                }
        }
 }
 
 
-func (E *Exporter) WriteScope(scope *Globals.Scope) {
+func (E *Exporter) WriteScope(scope *Globals.Scope, export_all bool) {
        if E.debug {
                print " {";
        }
 
        for p := scope.entries.first; p != nil; p = p.next {
-               if p.obj.exported {
+               if export_all || p.obj.exported {
                        E.WriteObject(p.obj);
                }
        }
-       E.WriteObjectTag(Object.EOS);
+       E.WriteObject(nil);
        
        if E.debug {
                print " }";
@@ -119,8 +119,9 @@ func (E *Exporter) WriteScope(scope *Globals.Scope) {
 
 
 func (E *Exporter) WriteObject(obj *Globals.Object) {
-       if !obj.exported {
-               panic "!obj.exported";
+       if obj == nil {
+               E.WriteObjectTag(Object.EOS);
+               return;
        }
 
        if obj.kind == Object.TYPE && obj.typ.obj == obj {
@@ -196,10 +197,10 @@ func (E *Exporter) WriteType(typ *Globals.Type) {
 
        case Type.FUNCTION:
                E.WriteInt(typ.flags);
-               E.WriteScope(typ.scope);
+               E.WriteScope(typ.scope, true);
                
        case Type.STRUCT, Type.INTERFACE:
-               E.WriteScope(typ.scope);
+               E.WriteScope(typ.scope, true);  // for now
 
        case Type.POINTER, Type.REFERENCE:
                E.WriteType(typ.elt);
@@ -220,11 +221,7 @@ func (E *Exporter) WritePackage(pno int) {
                return;
        }
 
-       if -Object.PACKAGE >= 0 {
-               panic "-Object.PACKAGE >= 0";  // conflict with ref numbers
-       }
-       
-       E.WritePackageTag(-Object.PACKAGE);
+       E.WritePackageTag(-1);
        pkg.ref = E.pkg_ref;
        E.pkg_ref++;
 
@@ -259,7 +256,7 @@ func (E *Exporter) Export(comp* Globals.Compilation, file_name string) {
        
        pkg := comp.pkgs[0];
        E.WritePackage(0);
-       E.WriteScope(pkg.scope);
+       E.WriteScope(pkg.scope, false);
        
        if E.debug {
                print "\n(", E.buf_pos, " bytes)\n";
index 5b3d6c1d903aa279e885e9784ca4a2f3ab49704a..90214d6d10bfd021f4d75a0c76d4d9e34ae8f630 100644 (file)
@@ -16,7 +16,7 @@ package Globals
 export Object
 type Object struct {
        exported bool;
-       pos int;  // source position
+       pos int;  // source position (< 0 if unknown position)
        kind int;
        ident string;
        typ *Type;
@@ -270,8 +270,7 @@ func (L *List) AddTyp(typ *Type) {
 // Scope methods
 
 func (scope *Scope) Lookup(ident string) *Object {
-       var p *Elem;
-       for p = scope.entries.first; p != nil; p = p.next {
+       for p := scope.entries.first; p != nil; p = p.next {
                if p.obj.ident == ident {
                        if p.obj.scope != scope {
                                panic "incorrect scope for object";
@@ -307,8 +306,7 @@ func (scope *Scope) InsertImport(obj *Object) *Object {
 
 func (scope *Scope) Print() {
        print "scope {";
-       var p* Elem;
-       for p = scope.entries.first; p != nil; p = p.next {
+       for p := scope.entries.first; p != nil; p = p.next {
                print "\n  ", p.obj.ident;
        }
        print "\n}\n";
index 79ffec1022fa2322aa29bc7c000c4477d544b2d7..5f086fdcf6e31e3fa4bf4ec3cfd37d954fa1b8c7 100755 (executable)
@@ -24,7 +24,7 @@ type Importer struct {
 
 
 func (I *Importer) ReadType() *Globals.Type;
-func (I *Importer) ReadObject(tag int) *Globals.Object;
+func (I *Importer) ReadObject() *Globals.Object;
 func (I *Importer) ReadPackage() *Globals.Package;
 
 
@@ -105,7 +105,7 @@ func (I *Importer) ReadPackageTag() int {
                if tag >= 0 {
                        print " [P", tag, "]";  // package ref
                } else {
-                       print "\nP", I.pkg_ref, ": ", -tag;  // package tag
+                       print "\nP", I.pkg_ref, ":";
                }
        }
        return tag;
@@ -118,14 +118,12 @@ func (I *Importer) ReadScope() *Globals.Scope {
        }
 
        scope := Globals.NewScope(nil);
-       for {
-               tag := I.ReadObjectTag();
-               if tag == Object.EOS {  // terminator
-                       break;
-               }
+       obj := I.ReadObject();
+       for obj != nil {
                // InsertImport only needed for package scopes
                // but ok to use always
-               scope.InsertImport(I.ReadObject(tag));
+               scope.InsertImport(obj);
+               obj = I.ReadObject();
        }
        
        if I.debug {
@@ -136,7 +134,12 @@ func (I *Importer) ReadScope() *Globals.Scope {
 }
 
 
-func (I *Importer) ReadObject(tag int) *Globals.Object {
+func (I *Importer) ReadObject() *Globals.Object {
+       tag := I.ReadObjectTag();
+       if tag == Object.EOS {
+               return nil;
+       }
+       
        if tag == Object.PTYPE {
                // primary type object - handled entirely by ReadType()
                typ := I.ReadType();
@@ -175,7 +178,6 @@ func (I *Importer) ReadObject(tag int) *Globals.Object {
 
 func (I *Importer) ReadType() *Globals.Type {
        tag := I.ReadTypeTag();
-
        if tag >= 0 {
                return I.types[tag];  // type already imported
        }
@@ -235,15 +237,10 @@ func (I *Importer) ReadType() *Globals.Type {
 
 func (I *Importer) ReadPackage() *Globals.Package {
        tag := I.ReadPackageTag();
-
        if tag >= 0 {
                return I.pkgs[tag];  // package already imported
        }
 
-       if -tag != Object.PACKAGE {
-               panic "incorrect package tag";
-       }
-       
        ident := I.ReadString();
        file_name := I.ReadString();
        key := I.ReadString();
@@ -295,14 +292,11 @@ func (I *Importer) Import(comp* Globals.Compilation, file_name string) *Globals.
        }
 
        pkg := I.ReadPackage();
-       for {
-               tag := I.ReadObjectTag();
-               if tag == Object.EOS {
-                       break;
-               }
-               obj := I.ReadObject(tag);
+       obj := I.ReadObject();
+       for obj != nil {
                obj.pnolev = pkg.obj.pnolev;
                pkg.scope.InsertImport(obj);
+               obj = I.ReadObject();
        }
 
        if I.debug {
index 77062a9125243bbf7c47e9d9f6484dd9b4b8880a..02081d3a4475914975d962b69c2589b060a88434 100644 (file)
@@ -595,7 +595,7 @@ func (P *Parser) ParseFunctionType() *Globals.Type {
 }
 
 
-func (P *Parser) ParseMethodDecl() {
+func (P *Parser) ParseMethodDecl(recv_typ *Globals.Type) {
        P.Trace("MethodDecl");
        
        pos := P.pos;
@@ -603,9 +603,14 @@ func (P *Parser) ParseMethodDecl() {
        P.OpenScope();
        P.level--;
        sig := P.top_scope;
+       
        // dummy receiver (give it a name so it won't conflict with unnamed result)
-       sig.Insert(Globals.NewObject(pos, Object.VAR, ".recv"));
+       recv := Globals.NewObject(pos, Object.VAR, ".recv");
+       recv.typ = recv_typ;
+       sig.Insert(recv);
+       
        P.ParseParameters();
+       
        r0 := sig.entries.len_;
        P.TryResult();
        P.level++;
@@ -630,7 +635,7 @@ func (P *Parser) ParseInterfaceType() *Globals.Type {
        typ := Globals.NewType(Type.INTERFACE);
        typ.scope = P.top_scope;
        for P.tok == Scanner.IDENT {
-               P.ParseMethodDecl();
+               P.ParseMethodDecl(typ);
        }
        P.level++;
        P.CloseScope();
index 4ed02774886ace9d3385c71a6d46d374ffea3477..101c42a3510ab17a3d9831e17155bde2a7532d30 100755 (executable)
@@ -130,16 +130,16 @@ func init() {
        any_t = Register(DeclType(Type.ANY, "any", 8));
 
        // All but 'byte' should be platform-dependent, eventually.
-       byte_t = DeclAlias("byte", uint8_t);
-       ushort_t = DeclAlias("ushort", uint16_t);
-       uint_t = DeclAlias("uint", uint32_t);
-       ulong_t = DeclAlias("ulong", uint32_t);
-       short_t = DeclAlias("short", int16_t);
-       int_t = DeclAlias("int", int32_t);
-       long_t = DeclAlias("long", int32_t);
-       float_t = DeclAlias("float", float32_t);
-       double_t = DeclAlias("double", float64_t);
-       ptrint_t = DeclAlias("ptrint", uint64_t);
+       byte_t = Register(DeclAlias("byte", uint8_t));
+       ushort_t = Register(DeclAlias("ushort", uint16_t));
+       uint_t = Register(DeclAlias("uint", uint32_t));
+       ulong_t = Register(DeclAlias("ulong", uint32_t));
+       short_t = Register(DeclAlias("short", int16_t));
+       int_t = Register(DeclAlias("int", int32_t));
+       long_t = Register(DeclAlias("long", int32_t));
+       float_t = Register(DeclAlias("float", float32_t));
+       double_t = Register(DeclAlias("double", float64_t));
+       ptrint_t = Register(DeclAlias("ptrint", uint64_t));
 
        // Predeclared constants
        true_ = DeclObj(Object.CONST, "true", bool_t);