Add a nullptr literal

This commit is contained in:
Nigel Tao committed 2018-07-15 16:41:23 +10:00
1 parent fdf152f8b3
commit 15f2f42b77
8 files changed
+88 -44

No files matched your search

+8
View File
@@ -701,6 +701,10 @@ func (n *TypeExpr) IsIOType() bool {
return n.id0 == 0 && n.id1 == t.IDBase && (n.id2 == t.IDIOReader || n.id2 == t.IDIOWriter)
}
func (n *TypeExpr) IsNullptr() bool {
return n.id0 == 0 && n.id1 == t.IDBase && n.id2 == t.IDQNullptr
}
func (n *TypeExpr) IsNumType() bool {
return n.id0 == 0 && n.id1 == t.IDBase && n.id2.IsNumType()
}
@@ -717,6 +721,10 @@ func (n *TypeExpr) IsArrayType() bool {
return n.id0 == t.IDArray
}
func (n *TypeExpr) IsPointerType() bool {
return n.id0 == t.IDNptr || n.id0 == t.IDPtr
}
func (n *TypeExpr) IsSliceType() bool {
return n.id0 == t.IDSlice
}
+1
View File
@@ -38,6 +38,7 @@ var (
typeExprGeneric2 = a.NewTypeExpr(0, t.IDBase, t.IDDagger2, nil, nil, nil)
typeExprIdeal = a.NewTypeExpr(0, t.IDBase, t.IDQIdeal, nil, nil, nil)
typeExprList = a.NewTypeExpr(0, t.IDBase, t.IDDollar, nil, nil, nil)
typeExprNullptr = a.NewTypeExpr(0, t.IDBase, t.IDQNullptr, nil, nil, nil)
typeExprPlaceholder = a.NewTypeExpr(0, t.IDBase, t.IDQPlaceholder, nil, nil, nil)
typeExprTypeExpr = a.NewTypeExpr(0, t.IDBase, t.IDQTypeExpr, nil, nil, nil)
+28 -9
View File
@@ -404,6 +404,11 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
n.SetConstValue(one)
n.SetMType(typeExprBool)
return nil
case t.IDNullptr:
n.SetConstValue(zero)
n.SetMType(typeExprNullptr)
return nil
}
case t.IDOpenParen, t.IDTry:
@@ -735,6 +740,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
}
rTyp := rhs.MType()
pointerComparison := false
switch op {
case t.IDXBinaryAnd, t.IDXBinaryOr:
if !lTyp.IsBool() {
@@ -745,24 +751,37 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type",
op.AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm))
}
case t.IDXBinaryNotEq, t.IDXBinaryEqEq:
if lTyp.Eq(typeExprStatus) && rTyp.Eq(typeExprStatus) {
break
}
fallthrough
default:
bad := (*a.Expr)(nil)
if !lTyp.IsNumTypeOrIdeal() {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type",
op.AmbiguousForm().Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm))
bad = lhs
} else if !rTyp.IsNumTypeOrIdeal() {
bad = rhs
}
if !rTyp.IsNumTypeOrIdeal() {
if op == t.IDXBinaryNotEq || op == t.IDXBinaryEqEq {
if lTyp.Eq(typeExprStatus) && rTyp.Eq(typeExprStatus) {
break
}
lNullptr := lTyp.Eq(typeExprNullptr)
rNullptr := rTyp.Eq(typeExprNullptr)
if (lNullptr && rNullptr) ||
(lNullptr && rTyp.IsPointerType()) ||
(rNullptr && lTyp.IsPointerType()) {
pointerComparison = true
break
}
}
if bad != nil {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type",
op.AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm))
op.AmbiguousForm().Str(q.tm), bad.Str(q.tm), bad.MType().Str(q.tm))
}
}
switch op {
default:
if pointerComparison {
break
}
if !lTyp.EqIgnoringRefinements(rTyp) && !lTyp.IsIdeal() && !rTyp.IsIdeal() {
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, do not have compatible types",
op.AmbiguousForm().Str(q.tm),
+34 -25
View File
@@ -371,22 +371,24 @@ const (
const (
minBuiltInLiteral = 0xB0
minBuiltInNumLiteral = 0xB2
minBuiltInNumLiteral = 0xB3
maxBuiltInNumLiteral = 0xBF
maxBuiltInLiteral = 0xBF
IDFalse = ID(0xB0)
IDTrue = ID(0xB1)
ID0 = ID(0xB2)
ID1 = ID(0xB3)
ID2 = ID(0xB4)
ID4 = ID(0xB5)
ID8 = ID(0xB6)
ID16 = ID(0xB7)
ID32 = ID(0xB8)
ID64 = ID(0xB9)
ID128 = ID(0xBA)
ID256 = ID(0xBB)
IDFalse = ID(0xB0)
IDTrue = ID(0xB1)
IDNullptr = ID(0xB2)
ID0 = ID(0xB3)
ID1 = ID(0xB4)
ID2 = ID(0xB5)
ID4 = ID(0xB6)
ID8 = ID(0xB7)
ID16 = ID(0xB8)
ID32 = ID(0xB9)
ID64 = ID(0xBA)
ID128 = ID(0xBB)
ID256 = ID(0xBC)
)
const (
@@ -421,6 +423,7 @@ const (
IDDagger1 = ID(0x11A)
IDDagger2 = ID(0x11B)
IDQNullptr = ID(0x11C)
IDQPlaceholder = ID(0x11D)
IDQTypeExpr = ID(0x11E)
@@ -648,18 +651,20 @@ var builtInsByID = [nBuiltInIDs]string{
IDSlice: "slice",
IDTable: "table",
IDFalse: "false",
IDTrue: "true",
ID0: "0",
ID1: "1",
ID2: "2",
ID4: "4",
ID8: "8",
ID16: "16",
ID32: "32",
ID64: "64",
ID128: "128",
ID256: "256",
IDFalse: "false",
IDTrue: "true",
IDNullptr: "nullptr",
ID0: "0",
ID1: "1",
ID2: "2",
ID4: "4",
ID8: "8",
ID16: "16",
ID32: "32",
ID64: "64",
ID128: "128",
ID256: "256",
// -------- 0x100 block.
@@ -692,6 +697,10 @@ var builtInsByID = [nBuiltInIDs]string{
IDDagger1: "†", // U+2020 DAGGER
IDDagger2: "‡", // U+2021 DOUBLE DAGGER
// IDQNullptr is used by the type checker to build an artificial MType for
// the nullptr literal.
IDQNullptr: "«Nullptr»",
// IDQPlaceholder is used by the type checker to build an artificial MType
// for AST nodes that aren't expression nodes or type expression nodes,
// such as struct definition nodes and statement nodes. Its presence means