Commit 0abbb8c7 authored by Robert Griesemer's avatar Robert Griesemer

more import/export stuff:

- no need to import/export predeclared types
- fix for receiver types
- cleanups
- added tests to Makefile

R=r
OCL=13728
CL=13730
parent 54c8948f
...@@ -94,23 +94,23 @@ func (E *Exporter) WritePackageTag(tag int) { ...@@ -94,23 +94,23 @@ func (E *Exporter) WritePackageTag(tag int) {
if tag >= 0 { if tag >= 0 {
print " [P", tag, "]"; // package ref print " [P", tag, "]"; // package ref
} else { } 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 { if E.debug {
print " {"; print " {";
} }
for p := scope.entries.first; p != nil; p = p.next { 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.WriteObject(p.obj);
} }
} }
E.WriteObjectTag(Object.EOS); E.WriteObject(nil);
if E.debug { if E.debug {
print " }"; print " }";
...@@ -119,8 +119,9 @@ func (E *Exporter) WriteScope(scope *Globals.Scope) { ...@@ -119,8 +119,9 @@ func (E *Exporter) WriteScope(scope *Globals.Scope) {
func (E *Exporter) WriteObject(obj *Globals.Object) { func (E *Exporter) WriteObject(obj *Globals.Object) {
if !obj.exported { if obj == nil {
panic "!obj.exported"; E.WriteObjectTag(Object.EOS);
return;
} }
if obj.kind == Object.TYPE && obj.typ.obj == obj { if obj.kind == Object.TYPE && obj.typ.obj == obj {
...@@ -196,10 +197,10 @@ func (E *Exporter) WriteType(typ *Globals.Type) { ...@@ -196,10 +197,10 @@ func (E *Exporter) WriteType(typ *Globals.Type) {
case Type.FUNCTION: case Type.FUNCTION:
E.WriteInt(typ.flags); E.WriteInt(typ.flags);
E.WriteScope(typ.scope); E.WriteScope(typ.scope, true);
case Type.STRUCT, Type.INTERFACE: case Type.STRUCT, Type.INTERFACE:
E.WriteScope(typ.scope); E.WriteScope(typ.scope, true); // for now
case Type.POINTER, Type.REFERENCE: case Type.POINTER, Type.REFERENCE:
E.WriteType(typ.elt); E.WriteType(typ.elt);
...@@ -220,11 +221,7 @@ func (E *Exporter) WritePackage(pno int) { ...@@ -220,11 +221,7 @@ func (E *Exporter) WritePackage(pno int) {
return; return;
} }
if -Object.PACKAGE >= 0 { E.WritePackageTag(-1);
panic "-Object.PACKAGE >= 0"; // conflict with ref numbers
}
E.WritePackageTag(-Object.PACKAGE);
pkg.ref = E.pkg_ref; pkg.ref = E.pkg_ref;
E.pkg_ref++; E.pkg_ref++;
...@@ -259,7 +256,7 @@ func (E *Exporter) Export(comp* Globals.Compilation, file_name string) { ...@@ -259,7 +256,7 @@ func (E *Exporter) Export(comp* Globals.Compilation, file_name string) {
pkg := comp.pkgs[0]; pkg := comp.pkgs[0];
E.WritePackage(0); E.WritePackage(0);
E.WriteScope(pkg.scope); E.WriteScope(pkg.scope, false);
if E.debug { if E.debug {
print "\n(", E.buf_pos, " bytes)\n"; print "\n(", E.buf_pos, " bytes)\n";
......
...@@ -16,7 +16,7 @@ package Globals ...@@ -16,7 +16,7 @@ package Globals
export Object export Object
type Object struct { type Object struct {
exported bool; exported bool;
pos int; // source position pos int; // source position (< 0 if unknown position)
kind int; kind int;
ident string; ident string;
typ *Type; typ *Type;
...@@ -270,8 +270,7 @@ func (L *List) AddTyp(typ *Type) { ...@@ -270,8 +270,7 @@ func (L *List) AddTyp(typ *Type) {
// Scope methods // Scope methods
func (scope *Scope) Lookup(ident string) *Object { 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.ident == ident {
if p.obj.scope != scope { if p.obj.scope != scope {
panic "incorrect scope for object"; panic "incorrect scope for object";
...@@ -307,8 +306,7 @@ func (scope *Scope) InsertImport(obj *Object) *Object { ...@@ -307,8 +306,7 @@ func (scope *Scope) InsertImport(obj *Object) *Object {
func (scope *Scope) Print() { func (scope *Scope) Print() {
print "scope {"; 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 ", p.obj.ident;
} }
print "\n}\n"; print "\n}\n";
......
...@@ -24,7 +24,7 @@ type Importer struct { ...@@ -24,7 +24,7 @@ type Importer struct {
func (I *Importer) ReadType() *Globals.Type; 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; func (I *Importer) ReadPackage() *Globals.Package;
...@@ -105,7 +105,7 @@ func (I *Importer) ReadPackageTag() int { ...@@ -105,7 +105,7 @@ func (I *Importer) ReadPackageTag() int {
if tag >= 0 { if tag >= 0 {
print " [P", tag, "]"; // package ref print " [P", tag, "]"; // package ref
} else { } else {
print "\nP", I.pkg_ref, ": ", -tag; // package tag print "\nP", I.pkg_ref, ":";
} }
} }
return tag; return tag;
...@@ -118,14 +118,12 @@ func (I *Importer) ReadScope() *Globals.Scope { ...@@ -118,14 +118,12 @@ func (I *Importer) ReadScope() *Globals.Scope {
} }
scope := Globals.NewScope(nil); scope := Globals.NewScope(nil);
for { obj := I.ReadObject();
tag := I.ReadObjectTag(); for obj != nil {
if tag == Object.EOS { // terminator
break;
}
// InsertImport only needed for package scopes // InsertImport only needed for package scopes
// but ok to use always // but ok to use always
scope.InsertImport(I.ReadObject(tag)); scope.InsertImport(obj);
obj = I.ReadObject();
} }
if I.debug { if I.debug {
...@@ -136,7 +134,12 @@ func (I *Importer) ReadScope() *Globals.Scope { ...@@ -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 { if tag == Object.PTYPE {
// primary type object - handled entirely by ReadType() // primary type object - handled entirely by ReadType()
typ := I.ReadType(); typ := I.ReadType();
...@@ -175,7 +178,6 @@ func (I *Importer) ReadObject(tag int) *Globals.Object { ...@@ -175,7 +178,6 @@ func (I *Importer) ReadObject(tag int) *Globals.Object {
func (I *Importer) ReadType() *Globals.Type { func (I *Importer) ReadType() *Globals.Type {
tag := I.ReadTypeTag(); tag := I.ReadTypeTag();
if tag >= 0 { if tag >= 0 {
return I.types[tag]; // type already imported return I.types[tag]; // type already imported
} }
...@@ -235,15 +237,10 @@ func (I *Importer) ReadType() *Globals.Type { ...@@ -235,15 +237,10 @@ func (I *Importer) ReadType() *Globals.Type {
func (I *Importer) ReadPackage() *Globals.Package { func (I *Importer) ReadPackage() *Globals.Package {
tag := I.ReadPackageTag(); tag := I.ReadPackageTag();
if tag >= 0 { if tag >= 0 {
return I.pkgs[tag]; // package already imported return I.pkgs[tag]; // package already imported
} }
if -tag != Object.PACKAGE {
panic "incorrect package tag";
}
ident := I.ReadString(); ident := I.ReadString();
file_name := I.ReadString(); file_name := I.ReadString();
key := I.ReadString(); key := I.ReadString();
...@@ -295,14 +292,11 @@ func (I *Importer) Import(comp* Globals.Compilation, file_name string) *Globals. ...@@ -295,14 +292,11 @@ func (I *Importer) Import(comp* Globals.Compilation, file_name string) *Globals.
} }
pkg := I.ReadPackage(); pkg := I.ReadPackage();
for { obj := I.ReadObject();
tag := I.ReadObjectTag(); for obj != nil {
if tag == Object.EOS {
break;
}
obj := I.ReadObject(tag);
obj.pnolev = pkg.obj.pnolev; obj.pnolev = pkg.obj.pnolev;
pkg.scope.InsertImport(obj); pkg.scope.InsertImport(obj);
obj = I.ReadObject();
} }
if I.debug { if I.debug {
......
...@@ -595,7 +595,7 @@ func (P *Parser) ParseFunctionType() *Globals.Type { ...@@ -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"); P.Trace("MethodDecl");
pos := P.pos; pos := P.pos;
...@@ -603,9 +603,14 @@ func (P *Parser) ParseMethodDecl() { ...@@ -603,9 +603,14 @@ func (P *Parser) ParseMethodDecl() {
P.OpenScope(); P.OpenScope();
P.level--; P.level--;
sig := P.top_scope; sig := P.top_scope;
// dummy receiver (give it a name so it won't conflict with unnamed result) // 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(); P.ParseParameters();
r0 := sig.entries.len_; r0 := sig.entries.len_;
P.TryResult(); P.TryResult();
P.level++; P.level++;
...@@ -630,7 +635,7 @@ func (P *Parser) ParseInterfaceType() *Globals.Type { ...@@ -630,7 +635,7 @@ func (P *Parser) ParseInterfaceType() *Globals.Type {
typ := Globals.NewType(Type.INTERFACE); typ := Globals.NewType(Type.INTERFACE);
typ.scope = P.top_scope; typ.scope = P.top_scope;
for P.tok == Scanner.IDENT { for P.tok == Scanner.IDENT {
P.ParseMethodDecl(); P.ParseMethodDecl(typ);
} }
P.level++; P.level++;
P.CloseScope(); P.CloseScope();
......
...@@ -130,16 +130,16 @@ func init() { ...@@ -130,16 +130,16 @@ func init() {
any_t = Register(DeclType(Type.ANY, "any", 8)); any_t = Register(DeclType(Type.ANY, "any", 8));
// All but 'byte' should be platform-dependent, eventually. // All but 'byte' should be platform-dependent, eventually.
byte_t = DeclAlias("byte", uint8_t); byte_t = Register(DeclAlias("byte", uint8_t));
ushort_t = DeclAlias("ushort", uint16_t); ushort_t = Register(DeclAlias("ushort", uint16_t));
uint_t = DeclAlias("uint", uint32_t); uint_t = Register(DeclAlias("uint", uint32_t));
ulong_t = DeclAlias("ulong", uint32_t); ulong_t = Register(DeclAlias("ulong", uint32_t));
short_t = DeclAlias("short", int16_t); short_t = Register(DeclAlias("short", int16_t));
int_t = DeclAlias("int", int32_t); int_t = Register(DeclAlias("int", int32_t));
long_t = DeclAlias("long", int32_t); long_t = Register(DeclAlias("long", int32_t));
float_t = DeclAlias("float", float32_t); float_t = Register(DeclAlias("float", float32_t));
double_t = DeclAlias("double", float64_t); double_t = Register(DeclAlias("double", float64_t));
ptrint_t = DeclAlias("ptrint", uint64_t); ptrint_t = Register(DeclAlias("ptrint", uint64_t));
// Predeclared constants // Predeclared constants
true_ = DeclObj(Object.CONST, "true", bool_t); true_ = DeclObj(Object.CONST, "true", bool_t);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment