Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add basic support for nil (conversion, binary operations)

  • Loading branch information...
commit aee69d82a50839cf5cd1f16141fd0a4b58e21ee4 1 parent 6061fb0
@axw authored
View
2  compiler.go
@@ -90,7 +90,7 @@ func (c *compiler) Resolve(obj *ast.Object) Value {
case *types.Name:
typ = x.Underlying.(*types.Basic)
}
- value = &ConstValue{*(obj.Data.(*types.Const)), c, typ}
+ value = ConstValue{*(obj.Data.(*types.Const)), c, typ}
obj.Data = value
}
View
2  types/const.go
@@ -148,6 +148,8 @@ func (x *Const) Convert(typ *Type) *Const {
func (x *Const) String() string {
switch x := x.Val.(type) {
+ case nil:
+ return "nil"
case bool:
if x {
return "true"
View
3  types/types.go
@@ -21,7 +21,8 @@ type BasicTypeKind int
// Constants for basic types.
const (
- Uint8Kind BasicTypeKind = iota
+ NilKind BasicTypeKind = iota
+ Uint8Kind
Uint16Kind
Uint32Kind
Uint64Kind
View
5 types/universe.go
@@ -108,7 +108,10 @@ func init() {
defConst("false").Data = &Const{false}
defConst("iota")
- defConst("nil")
+
+ nil_ := defConst("nil")
+ nil_.Data = &Const{}
+ nil_.Type = &Basic{NilKind}
defFun("append")
defFun("cap")
View
30 value.go
@@ -143,6 +143,10 @@ func (lhs *LLVMValue) BinaryOp(op token.Token, rhs_ Value) Value {
fallthrough
case types.UntypedComplexKind:
rhs = rhs.Convert(lhs.Type()).(ConstValue)
+ case types.NilKind:
+ // The conversion will result in an *LLVMValue.
+ rhs_llvm := rhs.Convert(lhs.Type()).(*LLVMValue)
+ return lhs.BinaryOp(op, rhs_llvm)
}
rhs_value := rhs.LLVMValue()
@@ -166,7 +170,7 @@ func (lhs *LLVMValue) BinaryOp(op token.Token, rhs_ Value) Value {
}
return lhs.compiler.NewLLVMValue(result, lhs.typ)
}
- panic("unimplemented")
+ panic("unreachable")
}
func (v *LLVMValue) UnaryOp(op token.Token) Value {
@@ -220,8 +224,7 @@ func (v *LLVMValue) Convert(typ types.Type) Value {
}
}
- if _, fromstruct := srctyp.(*types.Struct);
- srcname != nil && fromstruct {
+ if _, fromstruct := srctyp.(*types.Struct); srcname != nil && fromstruct {
// TODO check whether the functions in the struct take
// value or pointer receivers.
@@ -253,17 +256,17 @@ func (v *LLVMValue) Convert(typ types.Type) Value {
return methods[i].Name >= m.Name
})
if mi >= len(methods) || methods[mi].Name != m.Name {
- panic("Failed to locate method: " + m.Name)
+ panic("Failed to locate method: " + m.Name)
}
method_obj := methods[mi]
method := v.compiler.Resolve(method_obj).(*LLVMValue)
llvm_value := method.LLVMValue()
llvm_value = builder.CreateBitCast(
- llvm_value,
- element_types[i+1], "")
+ llvm_value,
+ element_types[i+1], "")
iface_struct = builder.CreateInsertValue(
- iface_struct, llvm_value,
- i+1, "")
+ iface_struct, llvm_value,
+ i+1, "")
}
return v.compiler.NewLLVMValue(iface_struct, interface_)
@@ -375,7 +378,16 @@ func (v ConstValue) Convert(typ types.Type) Value {
typ = name.Underlying
}
compiler := v.compiler
- return ConstValue{*v.Const.Convert(&typ), compiler, typ.(*types.Basic)}
+ if basic, ok := typ.(*types.Basic); ok {
+ return ConstValue{*v.Const.Convert(&typ), compiler, basic}
+ } else {
+ // Special case for 'nil'
+ if v.typ.Kind == types.NilKind {
+ zero := llvm.ConstNull(typ.LLVMType())
+ return compiler.NewLLVMValue(zero, typ)
+ }
+ panic("unhandled conversion")
+ }
}
return v
}
Please sign in to comment.
Something went wrong with that request. Please try again.