vikunja-api/vendor/honnef.co/go/tools/ir/lvalue.go

117 lines
2.9 KiB
Go
Raw Normal View History

2018-12-28 23:15:05 +01:00
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2020-05-29 22:15:21 +02:00
package ir
2018-12-28 23:15:05 +01:00
// lvalues are the union of addressable expressions and map-index
// expressions.
import (
"go/ast"
"go/types"
)
// An lvalue represents an assignable location that may appear on the
// left-hand side of an assignment. This is a generalization of a
// pointer to permit updates to elements of maps.
//
type lvalue interface {
2020-05-29 22:15:21 +02:00
store(fn *Function, v Value, source ast.Node) // stores v into the location
load(fn *Function, source ast.Node) Value // loads the contents of the location
address(fn *Function) Value // address of the location
typ() types.Type // returns the type of the location
2018-12-28 23:15:05 +01:00
}
// An address is an lvalue represented by a true pointer.
type address struct {
addr Value
2020-05-29 22:15:21 +02:00
expr ast.Expr // source syntax of the value (not address) [debug mode]
2018-12-28 23:15:05 +01:00
}
2020-05-29 22:15:21 +02:00
func (a *address) load(fn *Function, source ast.Node) Value {
return emitLoad(fn, a.addr, source)
2018-12-28 23:15:05 +01:00
}
2020-05-29 22:15:21 +02:00
func (a *address) store(fn *Function, v Value, source ast.Node) {
store := emitStore(fn, a.addr, v, source)
2018-12-28 23:15:05 +01:00
if a.expr != nil {
// store.Val is v, converted for assignability.
emitDebugRef(fn, a.expr, store.Val, false)
}
}
func (a *address) address(fn *Function) Value {
if a.expr != nil {
emitDebugRef(fn, a.expr, a.addr, true)
}
return a.addr
}
func (a *address) typ() types.Type {
return deref(a.addr.Type())
}
// An element is an lvalue represented by m[k], the location of an
2020-05-29 22:15:21 +02:00
// element of a map. These locations are not addressable
2018-12-28 23:15:05 +01:00
// since pointers cannot be formed from them, but they do support
2020-05-29 22:15:21 +02:00
// load() and store().
2018-12-28 23:15:05 +01:00
//
type element struct {
2020-05-29 22:15:21 +02:00
m, k Value // map
t types.Type // map element type
2018-12-28 23:15:05 +01:00
}
2020-05-29 22:15:21 +02:00
func (e *element) load(fn *Function, source ast.Node) Value {
l := &MapLookup{
2018-12-28 23:15:05 +01:00
X: e.m,
Index: e.k,
}
l.setType(e.t)
2020-05-29 22:15:21 +02:00
return fn.emit(l, source)
2018-12-28 23:15:05 +01:00
}
2020-05-29 22:15:21 +02:00
func (e *element) store(fn *Function, v Value, source ast.Node) {
2018-12-28 23:15:05 +01:00
up := &MapUpdate{
Map: e.m,
Key: e.k,
2020-05-29 22:15:21 +02:00
Value: emitConv(fn, v, e.t, source),
2018-12-28 23:15:05 +01:00
}
2020-05-29 22:15:21 +02:00
fn.emit(up, source)
2018-12-28 23:15:05 +01:00
}
func (e *element) address(fn *Function) Value {
2020-05-29 22:15:21 +02:00
panic("map elements are not addressable")
2018-12-28 23:15:05 +01:00
}
func (e *element) typ() types.Type {
return e.t
}
// A blank is a dummy variable whose name is "_".
// It is not reified: loads are illegal and stores are ignored.
//
type blank struct{}
2020-05-29 22:15:21 +02:00
func (bl blank) load(fn *Function, source ast.Node) Value {
2018-12-28 23:15:05 +01:00
panic("blank.load is illegal")
}
2020-05-29 22:15:21 +02:00
func (bl blank) store(fn *Function, v Value, source ast.Node) {
2018-12-28 23:15:05 +01:00
s := &BlankStore{
Val: v,
}
2020-05-29 22:15:21 +02:00
fn.emit(s, source)
2018-12-28 23:15:05 +01:00
}
func (bl blank) address(fn *Function) Value {
panic("blank var is not addressable")
}
func (bl blank) typ() types.Type {
// This should be the type of the blank Ident; the typechecker
// doesn't provide this yet, but fortunately, we don't need it
// yet either.
panic("blank.typ is unimplemented")
}