1
0
mirror of https://github.com/golang/go synced 2024-10-01 03:38:32 -06:00
go/pointer/testdata/structs.go
Alan Donovan 3b5de067a1 go.tools/pointer: reflection, part 1: maps, and some core features.
Core:
        reflect.TypeOf
        reflect.ValueOf
        reflect.Zero
        reflect.Value.Interface
Maps:
        (reflect.Value).MapIndex
        (reflect.Value).MapKeys
        (reflect.Value).SetMapIndex
        (*reflect.rtype).Elem
        (*reflect.rtype).Key

+ tests:
  pointer/testdata/mapreflect.go.
  oracle/testdata/src/main/reflection.go.

Interface objects (T, V...) have been renamed "tagged objects".

Abstraction: we model reflect.Value similar to
interface{}---as a pointer that points only to tagged
objects---but a reflect.Value may also point to an "indirect
tagged object", one in which the payload V is of type *T not T.
These are required because reflect.Values can hold lvalues,
e.g. when derived via Field() or Elem(), though we won't use
them till we get to structs and pointers.

Solving: each reflection intrinsic defines a new constraint
and resolution rule.  Because of the nature of reflection,
generalizing across types, the resolution rules dynamically
create additional complex constraints during solving, where
previously only simple (copy) constraints were created.
This requires some solver changes:

  The work done before the main solver loop (to attach new
  constraints to the graph) is now done before each iteration,
  in processNewConstraints.

  Its loop over constraints is broken into two passes:
  the first handles base (addr-of) constraints,
  the second handles simple and complex constraints.

  constraint.init() has been inlined.  The only behaviour that
  varies across constraints is ptr()

Sadly this will pessimize presolver optimisations, when we get
there; such is the price of reflection.

Objects: reflection intrinsics create objects (i.e. cause
memory allocations) with no SSA operation.  We will represent
them as the cgnode of the instrinsic (e.g. reflect.New), so we
extend Labels and node.data to represent objects as a product
(not sum) of ssa.Value and cgnode and pull this out into its
own type, struct object.  This simplifies a number of
invariants and saves space.  The ntObject flag is now
represented by obj!=nil; the other flags are moved into
object.

cgnodes are now always recorded in objects/Labels for which it
is appropriate (all but those for globals, constants and the
shared contours for functions).

Also:
- Prepopulate the flattenMemo cache to consider reflect.Value
  a fake pointer, not a struct.
- Improve accessors and documentation on type Label.
- @conctypes assertions renamed @types (since dyn. types needn't be concrete).
- add oracle 'describe' test on an interface (missing, an oversight).

R=crawshaw
CC=golang-dev
https://golang.org/cl/13418048
2013-09-16 09:49:10 -04:00

102 lines
2.0 KiB
Go

// +build ignore
package main
var unknown bool // defeat dead-code elimination
var p, q int
type A struct {
f *int
g interface{}
}
func (a A) m1() {
print(a.f) // @pointsto main.p
}
func (a *A) m2() {
print(a) // @pointsto complit.A@struct1s:9
print(a.f) // @pointsto main.p
}
type B struct {
h *int
A
}
func structs1() {
b := &B{ // @line struct1s
h: &q,
}
b.f = &p
b.g = b
print(b.h) // @pointsto main.q
print(b.f) // @pointsto main.p
print(b.g) // @types *B
ptr := &b.f
print(*ptr) // @pointsto main.p
b.m1()
b.m2()
}
// @calls main.structs1 -> (main.A).m1
// @calls main.structs1 -> (*main.A).m2
// @calls (*main.B).m1 -> (main.A).m1
// @calls (*main.B).m2 -> (*main.A).m2
type T struct {
x int
y int
}
type S struct {
a [3]T
b *[3]T
c [3]*T
}
func structs2() {
var s S // @line s2s
print(&s) // @pointsto s@s2s:6
print(&s.a) // @pointsto s.a@s2s:6
print(&s.a[0]) // @pointsto s.a[*]@s2s:6
print(&s.a[0].x) // @pointsto s.a[*].x@s2s:6
print(&s.a[0].y) // @pointsto s.a[*].y@s2s:6
print(&s.b) // @pointsto s.b@s2s:6
print(&s.b[0]) // @pointsto
print(&s.b[0].x) // @pointsto
print(&s.b[0].y) // @pointsto
print(&s.c) // @pointsto s.c@s2s:6
print(&s.c[0]) // @pointsto s.c[*]@s2s:6
print(&s.c[0].x) // @pointsto
print(&s.c[0].y) // @pointsto
var s2 S // @line s2s2
s2.b = new([3]T) // @line s2s2b
print(s2.b) // @pointsto new@s2s2b:12
print(&s2.b) // @pointsto s2.b@s2s2:6
print(&s2.b[0]) // @pointsto new[*]@s2s2b:12
print(&s2.b[0].x) // @pointsto new[*].x@s2s2b:12
print(&s2.b[0].y) // @pointsto new[*].y@s2s2b:12
print(&s2.c[0].x) // @pointsto
print(&s2.c[0].y) // @pointsto
var s3 S // @line s2s3
s3.c[2] = new(T) // @line s2s3c
print(s3.c) // @pointsto
print(&s3.c) // @pointsto s3.c@s2s3:6
print(s3.c[1]) // @pointsto new@s2s3c:15
print(&s3.c[1]) // @pointsto s3.c[*]@s2s3:6
print(&s3.c[1].x) // @pointsto new.x@s2s3c:15
print(&s3.c[1].y) // @pointsto new.y@s2s3c:15
}
func main() {
structs1()
structs2()
}