Commit 8fdc5b60 authored by Austin Clements's avatar Austin Clements

Give NamedTypes a public interface

R=rsc
APPROVED=rsc
DELTA=32  (14 added, 1 deleted, 17 changed)
OCL=34043
CL=34043
parent bf0130cc
...@@ -117,7 +117,7 @@ func TypeFromNative(t reflect.Type) Type { ...@@ -117,7 +117,7 @@ func TypeFromNative(t reflect.Type) Type {
} }
if nt != nil { if nt != nil {
nt.def = et; nt.Complete(et);
et = nt; et = nt;
} }
......
...@@ -876,7 +876,7 @@ func (a *exprInfo) compileSelectorExpr(v *expr, name string) *expr { ...@@ -876,7 +876,7 @@ func (a *exprInfo) compileSelectorExpr(v *expr, name string) *expr {
mark(depth, pathName + "." + name); mark(depth, pathName + "." + name);
log.Crash("Methods not implemented"); log.Crash("Methods not implemented");
} }
t = ti.def; t = ti.Def;
} }
// If it's a struct type, check fields and embedded types // If it's a struct type, check fields and embedded types
......
...@@ -960,16 +960,30 @@ type Method struct { ...@@ -960,16 +960,30 @@ type Method struct {
type NamedType struct { type NamedType struct {
token.Position; token.Position;
name string; Name string;
// Underlying type. If incomplete is true, this will be nil. // Underlying type. If incomplete is true, this will be nil.
// If incomplete is false and this is still nil, then this is // If incomplete is false and this is still nil, then this is
// a placeholder type representing an error. // a placeholder type representing an error.
def Type; Def Type;
// True while this type is being defined. // True while this type is being defined.
incomplete bool; incomplete bool;
methods map[string] Method; methods map[string] Method;
} }
// TODO(austin) This is temporarily needed by the debugger's remote
// type parser. This should only be possible with block.DefineType.
func NewNamedType(name string) *NamedType {
return &NamedType{token.Position{}, name, nil, true, make(map[string] Method)};
}
func (t *NamedType) Complete(def Type) {
if !t.incomplete {
log.Crashf("cannot complete already completed NamedType %+v", *t);
}
t.Def = def;
t.incomplete = false;
}
func (t *NamedType) compat(o Type, conv bool) bool { func (t *NamedType) compat(o Type, conv bool) bool {
t2, ok := o.(*NamedType); t2, ok := o.(*NamedType);
if ok { if ok {
...@@ -977,7 +991,7 @@ func (t *NamedType) compat(o Type, conv bool) bool { ...@@ -977,7 +991,7 @@ func (t *NamedType) compat(o Type, conv bool) bool {
// Two named types are conversion compatible // Two named types are conversion compatible
// if their literals are conversion // if their literals are conversion
// compatible. // compatible.
return t.def.compat(t2.def, conv); return t.Def.compat(t2.Def, conv);
} else { } else {
// Two named types are compatible if their // Two named types are compatible if their
// type names originate in the same type // type names originate in the same type
...@@ -987,23 +1001,23 @@ func (t *NamedType) compat(o Type, conv bool) bool { ...@@ -987,23 +1001,23 @@ func (t *NamedType) compat(o Type, conv bool) bool {
} }
// A named and an unnamed type are compatible if the // A named and an unnamed type are compatible if the
// respective type literals are compatible. // respective type literals are compatible.
return o.compat(t.def, conv); return o.compat(t.Def, conv);
} }
func (t *NamedType) lit() Type { func (t *NamedType) lit() Type {
return t.def.lit(); return t.Def.lit();
} }
func (t *NamedType) isBoolean() bool { func (t *NamedType) isBoolean() bool {
return t.def.isBoolean(); return t.Def.isBoolean();
} }
func (t *NamedType) isInteger() bool { func (t *NamedType) isInteger() bool {
return t.def.isInteger(); return t.Def.isInteger();
} }
func (t *NamedType) isFloat() bool { func (t *NamedType) isFloat() bool {
return t.def.isFloat(); return t.Def.isFloat();
} }
func (t *NamedType) isIdeal() bool { func (t *NamedType) isIdeal() bool {
...@@ -1011,11 +1025,11 @@ func (t *NamedType) isIdeal() bool { ...@@ -1011,11 +1025,11 @@ func (t *NamedType) isIdeal() bool {
} }
func (t *NamedType) String() string { func (t *NamedType) String() string {
return t.name; return t.Name;
} }
func (t *NamedType) Zero() Value { func (t *NamedType) Zero() Value {
return t.def.Zero(); return t.Def.Zero();
} }
/* /*
......
...@@ -43,7 +43,7 @@ func (a *typeCompiler) compileIdent(x *ast.Ident, allowRec bool) Type { ...@@ -43,7 +43,7 @@ func (a *typeCompiler) compileIdent(x *ast.Ident, allowRec bool) Type {
a.diagAt(x, "illegal recursive type"); a.diagAt(x, "illegal recursive type");
return nil; return nil;
} }
if !def.incomplete && def.def == nil { if !def.incomplete && def.Def == nil {
// Placeholder type from an earlier error // Placeholder type from an earlier error
return nil; return nil;
} }
...@@ -158,12 +158,12 @@ func (a *typeCompiler) compileStructType(x *ast.StructType, allowRec bool) Type ...@@ -158,12 +158,12 @@ func (a *typeCompiler) compileStructType(x *ast.StructType, allowRec bool) Type
// type name acts as the field identifier. // type name acts as the field identifier.
switch t := ts[i].(type) { switch t := ts[i].(type) {
case *NamedType: case *NamedType:
name = t.name; name = t.Name;
nt = t; nt = t;
case *PtrType: case *PtrType:
switch t := t.Elem.(type) { switch t := t.Elem.(type) {
case *NamedType: case *NamedType:
name = t.name; name = t.Name;
nt = t; nt = t;
} }
} }
...@@ -338,15 +338,14 @@ func (a *compiler) compileTypeDecl(b *block, decl *ast.GenDecl) bool { ...@@ -338,15 +338,14 @@ func (a *compiler) compileTypeDecl(b *block, decl *ast.GenDecl) bool {
} }
// Fill incomplete type // Fill incomplete type
if nt != nil { if nt != nil {
nt.(*NamedType).def = t; nt.(*NamedType).Complete(t);
nt.(*NamedType).incomplete = false;
} }
// Perform late type checking with complete type // Perform late type checking with complete type
if !tc.lateCheck() { if !tc.lateCheck() {
ok = false; ok = false;
if nt != nil { if nt != nil {
// Make the type a placeholder // Make the type a placeholder
nt.(*NamedType).def = nil; nt.(*NamedType).Def = nil;
} }
} }
} }
......
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