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

- fixed a bug w/ exports (wrong package info)

- keep track of type alias (type T1 T0) so we can print the proper type name

R=r
OCL=13688
CL=13688
This commit is contained in:
Robert Griesemer 2008-07-31 10:47:10 -07:00
parent 9fb9e82fa3
commit 0c374e9f89
9 changed files with 52 additions and 18 deletions

View File

@ -6,9 +6,14 @@
package base package base
type Foo int
type Bar *float;
type Node struct { type Node struct {
left, right *Node; left, right *Node;
val bool val bool;
f Foo
} }
export Node export Foo, Bar, Node

View File

@ -6,7 +6,7 @@
package decls package decls
import "base" //import "base"
import base "base" import base "base"
import base2 "base" import base2 "base"

View File

@ -23,7 +23,7 @@ type Exporter struct {
func (E *Exporter) WriteType(typ *Globals.Type); func (E *Exporter) WriteType(typ *Globals.Type);
func (E *Exporter) WriteObject(obj *Globals.Object); func (E *Exporter) WriteObject(obj *Globals.Object);
func (E *Exporter) WritePackage(pkg *Globals.Package); func (E *Exporter) WritePackage(pno int);
func (E *Exporter) WriteByte(x byte) { func (E *Exporter) WriteByte(x byte) {
@ -132,7 +132,7 @@ func (E *Exporter) WriteObject(obj *Globals.Object) {
E.WriteObjectTag(obj.kind); E.WriteObjectTag(obj.kind);
E.WriteString(obj.ident); E.WriteString(obj.ident);
E.WriteType(obj.typ); E.WriteType(obj.typ);
E.WritePackage(E.comp.pkgs[obj.pnolev]); E.WritePackage(obj.pnolev);
switch obj.kind { switch obj.kind {
case Object.CONST: case Object.CONST:
@ -173,12 +173,15 @@ func (E *Exporter) WriteType(typ *Globals.Type) {
panic "typ.obj.type() != typ"; // primary type panic "typ.obj.type() != typ"; // primary type
} }
E.WriteString(typ.obj.ident); E.WriteString(typ.obj.ident);
E.WritePackage(E.comp.pkgs[typ.obj.pnolev]); E.WritePackage(typ.obj.pnolev);
} else { } else {
E.WriteString(""); E.WriteString("");
} }
switch typ.form { switch typ.form {
case Type.ALIAS:
E.WriteType(typ.elt);
case Type.ARRAY: case Type.ARRAY:
E.WriteInt(typ.len_); E.WriteInt(typ.len_);
E.WriteType(typ.elt); E.WriteType(typ.elt);
@ -207,7 +210,11 @@ func (E *Exporter) WriteType(typ *Globals.Type) {
} }
func (E *Exporter) WritePackage(pkg *Globals.Package) { func (E *Exporter) WritePackage(pno int) {
if pno < 0 {
pno = 0;
}
pkg := E.comp.pkgs[pno];
if pkg.ref >= 0 { if pkg.ref >= 0 {
E.WritePackageTag(pkg.ref); // package already exported E.WritePackageTag(pkg.ref); // package already exported
return; return;
@ -251,7 +258,7 @@ func (E *Exporter) Export(comp* Globals.Compilation, file_name string) {
E.type_ref = Universe.types.len_; E.type_ref = Universe.types.len_;
pkg := comp.pkgs[0]; pkg := comp.pkgs[0];
E.WritePackage(pkg); E.WritePackage(0);
E.WriteScope(pkg.scope); E.WriteScope(pkg.scope);
if E.debug { if E.debug {

View File

@ -200,7 +200,9 @@ func (I *Importer) ReadType() *Globals.Type {
I.type_ref++; I.type_ref++;
switch (typ.form) { switch (typ.form) {
default: fallthrough; case Type.ALIAS:
typ.elt = I.ReadType();
case Type.ARRAY: case Type.ARRAY:
typ.len_ = I.ReadInt(); typ.len_ = I.ReadInt();
typ.elt = I.ReadType(); typ.elt = I.ReadType();

View File

@ -1633,9 +1633,21 @@ func (P *Parser) ParseTypeSpec(exported bool) {
P.Declare(obj); P.Declare(obj);
} }
typ := P.TryType(); // nil if we have an explicit forward declaration // If the next token is an identifier and we have a legal program,
// it must be a typename. In that case this declaration introduces
// an alias type.
make_alias := P.tok == Scanner.IDENT;
// If we have an explicit forward declaration, TryType will not
// find a type and return nil.
typ := P.TryType();
if typ != nil { if typ != nil {
if make_alias {
alias := Globals.NewType(Type.ALIAS);
alias.elt = typ;
typ = alias;
}
obj.typ = typ; obj.typ = typ;
if typ.obj == nil { if typ.obj == nil {
typ.obj = obj; // primary type object typ.obj = obj; // primary type object

View File

@ -199,6 +199,9 @@ func (P *Printer) PrintTypeStruct(typ *Globals.Type) {
} }
P.PrintType(typ); P.PrintType(typ);
case Type.ALIAS:
P.PrintType(typ.elt);
case Type.ARRAY: case Type.ARRAY:
print "[]"; print "[]";
P.PrintType(typ.elt); P.PrintType(typ.elt);

View File

@ -8,7 +8,7 @@ export
UNDEF, BAD, NIL, UNDEF, BAD, NIL,
BOOL, UINT, INT, FLOAT, STRING, BOOL, UINT, INT, FLOAT, STRING,
ANY, ANY,
ARRAY, STRUCT, INTERFACE, MAP, CHANNEL, FUNCTION, POINTER, REFERENCE ALIAS, ARRAY, STRUCT, INTERFACE, MAP, CHANNEL, FUNCTION, POINTER, REFERENCE
const /* form */ ( const /* form */ (
// internal types // internal types
@ -18,7 +18,7 @@ const /* form */ (
// 'any' type // 'any' type
ANY; ANY;
// composite types // composite types
ARRAY; STRUCT; INTERFACE; MAP; CHANNEL; FUNCTION; POINTER; REFERENCE; ALIAS; ARRAY; STRUCT; INTERFACE; MAP; CHANNEL; FUNCTION; POINTER; REFERENCE;
) )
@ -48,6 +48,7 @@ func FormStr(form int) string {
case FLOAT: return "FLOAT"; case FLOAT: return "FLOAT";
case STRING: return "STRING"; case STRING: return "STRING";
case ANY: return "ANY"; case ANY: return "ANY";
case ALIAS: return "ALIAS";
case ARRAY: return "ARRAY"; case ARRAY: return "ARRAY";
case STRUCT: return "STRUCT"; case STRUCT: return "STRUCT";
case INTERFACE: return "INTERFACE"; case INTERFACE: return "INTERFACE";

View File

@ -79,15 +79,17 @@ func DeclObj(kind int, ident string, typ *Globals.Type) *Globals.Object {
} }
func DeclAlias(ident string, typ *Globals.Type) *Globals.Type {
return DeclObj(Object.TYPE, ident, typ).typ;
}
func DeclType(form int, ident string, size int) *Globals.Type { func DeclType(form int, ident string, size int) *Globals.Type {
typ := Globals.NewType(form); typ := Globals.NewType(form);
typ.size = size; typ.size = size;
return DeclAlias(ident, typ); return DeclObj(Object.TYPE, ident, typ).typ;
}
func DeclAlias(ident string, typ *Globals.Type) *Globals.Type {
alias := Globals.NewType(Type.ALIAS);
alias.elt = typ;
return DeclObj(Object.TYPE, ident, alias).typ;
} }

View File

@ -50,6 +50,8 @@ func VerifyType(typ *Globals.Type) {
break; break;
case Type.ANY: case Type.ANY:
break; break;
case Type.ALIAS:
break;
case Type.ARRAY: case Type.ARRAY:
break; break;
case Type.STRUCT: case Type.STRUCT: