/
eval.go
110 lines (98 loc) · 3.2 KB
/
eval.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// 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.
// This file implements New, Eval and EvalNode.
package types
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"code.google.com/p/go.tools/go/exact"
)
// New is a convenience function to create a new type from a given
// expression or type literal string evaluated in Universe scope.
// New(str) is shorthand for Eval(str, nil, nil), but only returns
// the type result, and panics in case of an error.
// Position info for objects in the result type is undefined.
//
func New(str string) Type {
typ, _, err := Eval(str, nil, nil)
if err != nil {
panic(err)
}
return typ
}
// Eval returns the type and, if constant, the value for the
// expression or type literal string str evaluated in scope.
// If the expression contains function literals, the function
// bodies are ignored (though they must be syntactically correct).
//
// If pkg == nil, the Universe scope is used and the provided
// scope is ignored. Otherwise, the scope must belong to the
// package (either the package scope, or nested within the
// package scope).
//
// An error is returned if the scope is incorrect, the string
// has syntax errors, or if it cannot be evaluated in the scope.
// Position info for objects in the result type is undefined.
//
// Note: Eval should not be used instead of running Check to compute
// types and values, but in addition to Check. Eval will re-evaluate
// its argument each time, and it also does not know about the context
// in which an expression is used (e.g., an assignment). Thus, top-
// level untyped constants will return an untyped type rather then the
// respective context-specific type.
//
func Eval(str string, pkg *Package, scope *Scope) (typ Type, val exact.Value, err error) {
node, err := parser.ParseExpr(str)
if err != nil {
return nil, nil, err
}
// Create a file set that looks structurally identical to the
// one created by parser.ParseExpr for correct error positions.
fset := token.NewFileSet()
fset.AddFile("", len(str), fset.Base()).SetLinesForContent([]byte(str))
return EvalNode(fset, node, pkg, scope)
}
// EvalNode is like Eval but instead of string it accepts
// an expression node and respective file set.
//
// An error is returned if the scope is incorrect
// if the node cannot be evaluated in the scope.
//
func EvalNode(fset *token.FileSet, node ast.Expr, pkg *Package, scope *Scope) (typ Type, val exact.Value, err error) {
// verify package/scope relationship
if pkg == nil {
scope = Universe
} else {
s := scope
for s != nil && s != pkg.scope {
s = s.parent
}
// s == nil || s == pkg.scope
if s == nil {
return nil, nil, fmt.Errorf("scope does not belong to package %s", pkg.name)
}
}
// initialize checker
var conf Config
check := newChecker(&conf, fset, pkg)
check.topScope = scope
defer check.handleBailout(&err)
// evaluate node
var x operand
check.exprOrType(&x, node)
switch x.mode {
case invalid, novalue:
fallthrough
default:
unreachable() // or bailed out with error
case constant:
val = x.val
fallthrough
case typexpr, variable, mapindex, value, commaok:
typ = x.typ
}
return
}