summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/gopherjs/gopherjs/compiler/analysis/bool.go
blob: cba7c1c1497440e2532ceb42e40f97b6e5cbcdbe (plain)
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
package analysis

import (
	"go/ast"
	"go/constant"
	"go/token"
	"go/types"
)

func BoolValue(expr ast.Expr, info *types.Info) (bool, bool) {
	v := info.Types[expr].Value
	if v != nil && v.Kind() == constant.Bool {
		return constant.BoolVal(v), true
	}
	switch e := expr.(type) {
	case *ast.BinaryExpr:
		switch e.Op {
		case token.LAND:
			if b, ok := BoolValue(e.X, info); ok {
				if !b {
					return false, true
				}
				return BoolValue(e.Y, info)
			}
		case token.LOR:
			if b, ok := BoolValue(e.X, info); ok {
				if b {
					return true, true
				}
				return BoolValue(e.Y, info)
			}
		}
	case *ast.UnaryExpr:
		if e.Op == token.NOT {
			if b, ok := BoolValue(e.X, info); ok {
				return !b, true
			}
		}
	case *ast.ParenExpr:
		return BoolValue(e.X, info)
	}
	return false, false
}