github.com/tardisgo/tardisgo@v0.0.0-20161119180838-e0dd9a7e46b5/pogo/pointers.go (about)

     1  // Copyright 2014 Elliott Stoneham and The TARDIS Go Authors
     2  // Use of this source code is governed by an MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package pogo
     6  
     7  import (
     8  	"fmt"
     9  	"go/types"
    10  
    11  	"golang.org/x/tools/go/ssa"
    12  )
    13  
    14  // is this value a pointer?
    15  func valIsPointer(v interface{}) bool {
    16  	switch v.(type) {
    17  	case *ssa.Alloc, *ssa.FieldAddr, *ssa.Global, *ssa.IndexAddr:
    18  		return true
    19  	}
    20  	return false
    21  }
    22  
    23  // this is not language specific, it is used to show debug information
    24  func showIndirectValue(v interface{}) string {
    25  	switch v.(type) {
    26  	case *ssa.Alloc:
    27  		// TODO check if this is what to do here...
    28  		return fmt.Sprintf("%s", v.(*ssa.Alloc).Name())
    29  
    30  	case *ssa.FieldAddr:
    31  		st := v.(*ssa.FieldAddr).X.Type().Underlying().(*types.Pointer).Elem().Underlying().(*types.Struct)
    32  		// Be robust against a bad index.
    33  		name := "?"
    34  		if 0 <= v.(*ssa.FieldAddr).Field && v.(*ssa.FieldAddr).Field < st.NumFields() {
    35  			name = st.Field(v.(*ssa.FieldAddr).Field).Name()
    36  		}
    37  		return fmt.Sprintf("%s.%s [#%d]",
    38  			showIndirectValue(v.(*ssa.FieldAddr).X),
    39  			name, v.(*ssa.FieldAddr).Field)
    40  
    41  	case *ssa.IndexAddr:
    42  		return fmt.Sprintf("%s[%s]",
    43  			showIndirectValue(v.(*ssa.IndexAddr).X),
    44  			v.(*ssa.IndexAddr).Index.Name())
    45  
    46  	case *ssa.Global:
    47  		return fmt.Sprintf("%s", v.(*ssa.Global).Name()) //TODO qualify name
    48  
    49  	default:
    50  		//LogError("TODO", "pogo", fmt.Errorf("getIndirectValue() unhandled type: %v", reflect.TypeOf(v)))
    51  		return fmt.Sprintf("%s", v.(ssa.Value).String())
    52  	}
    53  }