Beispiel #1
0
// ident generates code for an identifier by loading it into the accumulator.
func (c *compiler) ident(e *ast.Ident, lv *arch.LV, tv types.TypeAndValue) *node {
	lv.Ident = true
	lv.Type = tv.Type.Underlying()
	lv.Name = e.Name

	_, isFunc := tv.Type.(*types.Signature)
	if isFunc {
		if x, ok := c.Uses[e]; ok {
			switch x := x.(type) {
			case *types.Func:
				lv.Storage = x.Storage()
				return newNode(opAddr, lv, nil, nil, nil)
			case *types.Fwrd:
				return newNode(opAddr, lv, nil, nil, nil)
			}
		}
	}

	v, found := c.variable(e, c.Uses)
	if !found {
		lv.Ident = false
		return nil
	}

	s, found := c.symbol(v)
	if !found {
		lv.Ident = false
		return nil
	}

	_, isRecord := lv.Type.(*types.Record)

	// arrays decay to pointers, so we need to get the original type
	// because the type and value by the typechecker is recorded as a pointer
	array, isArray := v.Type().(*types.Array)

	lv.Addr = s.Addr
	lv.Value = s.Value
	lv.Storage = v.Storage()
	switch {
	// constants
	case tv.Value != nil:
		lv.Value = tv.Value
		return newNode(opLit, lv, nil, nil, nil)

	case isArray:
		lv.Type = types.NewPointer(array.Elem(), nil)
		return newNode(opAddr, lv, nil, nil, nil)

	case isRecord:
		lv.Ident = false
		return newNode(opAddr, lv, nil, nil, nil)

	// variable that a integer or a pointer
	default:
		lv.Addressable = true
		return newNode(opIdent, lv, nil, nil, nil)
	}
}