2015-01-12 08:38:21 +01:00
|
|
|
package lang
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config/lang/ast"
|
|
|
|
)
|
|
|
|
|
2015-01-13 20:25:46 +01:00
|
|
|
// TypeCheck implements ast.Visitor for type checking an AST tree.
|
2015-01-12 08:38:21 +01:00
|
|
|
// It requires some configuration to look up the type of nodes.
|
2015-01-14 20:47:20 +01:00
|
|
|
//
|
|
|
|
// It also optionally will not type error and will insert an implicit
|
|
|
|
// type conversions for specific types if specified by the Implicit
|
|
|
|
// field. Note that this is kind of organizationally weird to put into
|
|
|
|
// this structure but we'd rather do that than duplicate the type checking
|
|
|
|
// logic multiple times.
|
2015-01-13 20:25:46 +01:00
|
|
|
type TypeCheck struct {
|
2015-01-15 05:58:46 +01:00
|
|
|
Scope ast.Scope
|
2015-01-12 08:38:21 +01:00
|
|
|
|
2015-01-14 20:47:20 +01:00
|
|
|
// Implicit is a map of implicit type conversions that we can do,
|
|
|
|
// and that shouldn't error. The key of the first map is the from type,
|
|
|
|
// the key of the second map is the to type, and the final string
|
|
|
|
// value is the function to call (which must be registered in the Scope).
|
|
|
|
Implicit map[ast.Type]map[ast.Type]string
|
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
// Stack of types. This shouldn't be used directly except by implementations
|
|
|
|
// of TypeCheckNode.
|
|
|
|
Stack []ast.Type
|
|
|
|
|
|
|
|
err error
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// TypeCheckNode is the interface that must be implemented by any
|
|
|
|
// ast.Node that wants to support type-checking. If the type checker
|
|
|
|
// encounters a node that doesn't implement this, it will error.
|
|
|
|
type TypeCheckNode interface {
|
|
|
|
TypeCheck(*TypeCheck) (ast.Node, error)
|
2015-01-12 08:38:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-13 20:25:46 +01:00
|
|
|
func (v *TypeCheck) Visit(root ast.Node) error {
|
2015-01-12 08:38:21 +01:00
|
|
|
v.lock.Lock()
|
|
|
|
defer v.lock.Unlock()
|
|
|
|
defer v.reset()
|
|
|
|
root.Accept(v.visit)
|
|
|
|
return v.err
|
|
|
|
}
|
|
|
|
|
2015-01-15 01:36:01 +01:00
|
|
|
func (v *TypeCheck) visit(raw ast.Node) ast.Node {
|
2015-01-12 08:38:21 +01:00
|
|
|
if v.err != nil {
|
2015-01-15 01:36:01 +01:00
|
|
|
return raw
|
2015-01-12 08:38:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
var result ast.Node
|
|
|
|
var err error
|
2015-01-12 08:38:21 +01:00
|
|
|
switch n := raw.(type) {
|
2015-02-26 20:32:39 +01:00
|
|
|
case *ast.Arithmetic:
|
|
|
|
tc := &typeCheckArithmetic{n}
|
|
|
|
result, err = tc.TypeCheck(v)
|
2015-01-12 08:38:21 +01:00
|
|
|
case *ast.Call:
|
2015-01-15 06:18:22 +01:00
|
|
|
tc := &typeCheckCall{n}
|
|
|
|
result, err = tc.TypeCheck(v)
|
2015-01-12 08:38:21 +01:00
|
|
|
case *ast.Concat:
|
2015-01-15 06:18:22 +01:00
|
|
|
tc := &typeCheckConcat{n}
|
|
|
|
result, err = tc.TypeCheck(v)
|
2015-01-12 08:38:21 +01:00
|
|
|
case *ast.LiteralNode:
|
2015-01-15 06:18:22 +01:00
|
|
|
tc := &typeCheckLiteral{n}
|
|
|
|
result, err = tc.TypeCheck(v)
|
2015-01-12 08:38:21 +01:00
|
|
|
case *ast.VariableAccess:
|
2015-01-15 06:18:22 +01:00
|
|
|
tc := &typeCheckVariableAccess{n}
|
|
|
|
result, err = tc.TypeCheck(v)
|
2015-01-12 08:38:21 +01:00
|
|
|
default:
|
2015-01-15 06:18:22 +01:00
|
|
|
tc, ok := raw.(TypeCheckNode)
|
|
|
|
if !ok {
|
2015-02-26 20:32:39 +01:00
|
|
|
err = fmt.Errorf("unknown node for type check: %#v", raw)
|
2015-01-15 06:18:22 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err = tc.TypeCheck(v)
|
2015-01-12 08:38:21 +01:00
|
|
|
}
|
2015-01-15 01:36:01 +01:00
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
if err != nil {
|
|
|
|
pos := raw.Pos()
|
|
|
|
v.err = fmt.Errorf("At column %d, line %d: %s",
|
|
|
|
pos.Column, pos.Line, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-02-26 20:32:39 +01:00
|
|
|
type typeCheckArithmetic struct {
|
|
|
|
n *ast.Arithmetic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *typeCheckArithmetic) TypeCheck(v *TypeCheck) (ast.Node, error) {
|
|
|
|
// The arguments are on the stack in reverse order, so pop them off.
|
2015-02-26 23:26:14 +01:00
|
|
|
exprs := make([]ast.Type, len(tc.n.Exprs))
|
2015-02-26 20:32:39 +01:00
|
|
|
for i, _ := range tc.n.Exprs {
|
2015-02-26 23:26:14 +01:00
|
|
|
exprs[len(tc.n.Exprs)-1-i] = v.StackPop()
|
2015-02-26 20:32:39 +01:00
|
|
|
}
|
|
|
|
|
2015-04-14 18:09:18 +02:00
|
|
|
// Determine the resulting type we want. We do this by going over
|
|
|
|
// every expression until we find one with a type we recognize.
|
|
|
|
// We do this because the first expr might be a string ("var.foo")
|
|
|
|
// and we need to know what to implicit to.
|
2015-02-26 23:26:14 +01:00
|
|
|
mathFunc := "__builtin_IntMath"
|
2015-02-26 20:32:39 +01:00
|
|
|
mathType := ast.TypeInt
|
2015-04-14 18:09:18 +02:00
|
|
|
for _, v := range exprs {
|
|
|
|
exit := true
|
|
|
|
switch v {
|
|
|
|
case ast.TypeInt:
|
|
|
|
mathFunc = "__builtin_IntMath"
|
|
|
|
mathType = v
|
|
|
|
case ast.TypeFloat:
|
|
|
|
mathFunc = "__builtin_FloatMath"
|
|
|
|
mathType = v
|
|
|
|
default:
|
|
|
|
exit = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// We found the type, so leave
|
|
|
|
if exit {
|
|
|
|
break
|
|
|
|
}
|
2015-02-26 20:32:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the args
|
2015-02-26 23:26:14 +01:00
|
|
|
for i, arg := range exprs {
|
2015-02-26 20:32:39 +01:00
|
|
|
if arg != mathType {
|
2015-02-26 23:26:14 +01:00
|
|
|
cn := v.ImplicitConversion(exprs[i], mathType, tc.n.Exprs[i])
|
2015-02-26 20:32:39 +01:00
|
|
|
if cn != nil {
|
|
|
|
tc.n.Exprs[i] = cn
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"operand %d should be %s, got %s",
|
|
|
|
i+1, mathType, arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 00:17:37 +01:00
|
|
|
// Modulo doesn't work for floats
|
|
|
|
if mathType == ast.TypeFloat && tc.n.Op == ast.ArithmeticOpMod {
|
|
|
|
return nil, fmt.Errorf("modulo cannot be used with floats")
|
|
|
|
}
|
|
|
|
|
2015-02-26 20:32:39 +01:00
|
|
|
// Return type
|
|
|
|
v.StackPush(mathType)
|
|
|
|
|
2015-02-26 23:26:14 +01:00
|
|
|
// Replace our node with a call to the proper function. This isn't
|
|
|
|
// type checked but we already verified types.
|
|
|
|
args := make([]ast.Node, len(tc.n.Exprs)+1)
|
|
|
|
args[0] = &ast.LiteralNode{
|
|
|
|
Value: tc.n.Op,
|
|
|
|
Typex: ast.TypeInt,
|
|
|
|
Posx: tc.n.Pos(),
|
|
|
|
}
|
|
|
|
copy(args[1:], tc.n.Exprs)
|
|
|
|
return &ast.Call{
|
|
|
|
Func: mathFunc,
|
|
|
|
Args: args,
|
|
|
|
Posx: tc.n.Pos(),
|
|
|
|
}, nil
|
2015-02-26 20:32:39 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
type typeCheckCall struct {
|
|
|
|
n *ast.Call
|
2015-01-12 08:38:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
func (tc *typeCheckCall) TypeCheck(v *TypeCheck) (ast.Node, error) {
|
2015-01-12 09:35:43 +01:00
|
|
|
// Look up the function in the map
|
2015-01-15 06:18:22 +01:00
|
|
|
function, ok := v.Scope.LookupFunc(tc.n.Func)
|
2015-01-12 09:35:43 +01:00
|
|
|
if !ok {
|
2015-01-15 06:18:22 +01:00
|
|
|
return nil, fmt.Errorf("unknown function called: %s", tc.n.Func)
|
2015-01-12 09:35:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The arguments are on the stack in reverse order, so pop them off.
|
2015-01-15 06:18:22 +01:00
|
|
|
args := make([]ast.Type, len(tc.n.Args))
|
|
|
|
for i, _ := range tc.n.Args {
|
|
|
|
args[len(tc.n.Args)-1-i] = v.StackPop()
|
2015-01-12 09:35:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the args
|
|
|
|
for i, expected := range function.ArgTypes {
|
2015-03-02 19:26:06 +01:00
|
|
|
if expected == ast.TypeAny {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-01-12 09:35:43 +01:00
|
|
|
if args[i] != expected {
|
2015-01-15 06:18:22 +01:00
|
|
|
cn := v.ImplicitConversion(args[i], expected, tc.n.Args[i])
|
2015-01-14 20:47:20 +01:00
|
|
|
if cn != nil {
|
2015-01-15 06:18:22 +01:00
|
|
|
tc.n.Args[i] = cn
|
2015-01-14 20:47:20 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
return nil, fmt.Errorf(
|
2015-01-12 09:35:43 +01:00
|
|
|
"%s: argument %d should be %s, got %s",
|
2015-01-15 06:18:22 +01:00
|
|
|
tc.n.Func, i+1, expected, args[i])
|
2015-01-12 09:35:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 21:40:47 +01:00
|
|
|
// If we're variadic, then verify the types there
|
2015-03-02 19:26:06 +01:00
|
|
|
if function.Variadic && function.VariadicType != ast.TypeAny {
|
2015-01-13 21:40:47 +01:00
|
|
|
args = args[len(function.ArgTypes):]
|
|
|
|
for i, t := range args {
|
|
|
|
if t != function.VariadicType {
|
2015-01-14 20:47:20 +01:00
|
|
|
realI := i + len(function.ArgTypes)
|
2015-01-15 06:18:22 +01:00
|
|
|
cn := v.ImplicitConversion(
|
|
|
|
t, function.VariadicType, tc.n.Args[realI])
|
2015-01-14 20:47:20 +01:00
|
|
|
if cn != nil {
|
2015-01-15 06:18:22 +01:00
|
|
|
tc.n.Args[realI] = cn
|
2015-01-14 20:47:20 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
return nil, fmt.Errorf(
|
2015-01-13 21:40:47 +01:00
|
|
|
"%s: argument %d should be %s, got %s",
|
2015-01-15 06:18:22 +01:00
|
|
|
tc.n.Func, realI,
|
|
|
|
function.VariadicType, t)
|
2015-01-13 21:40:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 09:35:43 +01:00
|
|
|
// Return type
|
2015-01-15 06:18:22 +01:00
|
|
|
v.StackPush(function.ReturnType)
|
|
|
|
|
|
|
|
return tc.n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type typeCheckConcat struct {
|
|
|
|
n *ast.Concat
|
2015-01-12 08:38:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
func (tc *typeCheckConcat) TypeCheck(v *TypeCheck) (ast.Node, error) {
|
|
|
|
n := tc.n
|
2015-01-12 08:38:21 +01:00
|
|
|
types := make([]ast.Type, len(n.Exprs))
|
|
|
|
for i, _ := range n.Exprs {
|
2015-01-15 06:18:22 +01:00
|
|
|
types[len(n.Exprs)-1-i] = v.StackPop()
|
2015-01-12 08:38:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// All concat args must be strings, so validate that
|
|
|
|
for i, t := range types {
|
|
|
|
if t != ast.TypeString {
|
2015-01-15 06:18:22 +01:00
|
|
|
cn := v.ImplicitConversion(t, ast.TypeString, n.Exprs[i])
|
2015-01-14 20:47:20 +01:00
|
|
|
if cn != nil {
|
|
|
|
n.Exprs[i] = cn
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"argument %d must be a string", i+1)
|
2015-01-12 08:38:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This always results in type string
|
2015-01-15 06:18:22 +01:00
|
|
|
v.StackPush(ast.TypeString)
|
|
|
|
|
|
|
|
return n, nil
|
2015-01-12 08:38:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
type typeCheckLiteral struct {
|
|
|
|
n *ast.LiteralNode
|
2015-01-12 08:38:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
func (tc *typeCheckLiteral) TypeCheck(v *TypeCheck) (ast.Node, error) {
|
|
|
|
v.StackPush(tc.n.Typex)
|
|
|
|
return tc.n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type typeCheckVariableAccess struct {
|
|
|
|
n *ast.VariableAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *typeCheckVariableAccess) TypeCheck(v *TypeCheck) (ast.Node, error) {
|
2015-01-12 08:38:21 +01:00
|
|
|
// Look up the variable in the map
|
2015-01-15 06:18:22 +01:00
|
|
|
variable, ok := v.Scope.LookupVar(tc.n.Name)
|
2015-01-12 08:38:21 +01:00
|
|
|
if !ok {
|
2015-01-15 06:18:22 +01:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"unknown variable accessed: %s", tc.n.Name)
|
2015-01-12 08:38:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the type to the stack
|
2015-01-15 06:18:22 +01:00
|
|
|
v.StackPush(variable.Type)
|
2015-01-12 08:38:21 +01:00
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
return tc.n, nil
|
2015-01-12 09:28:47 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
func (v *TypeCheck) ImplicitConversion(
|
2015-01-14 20:47:20 +01:00
|
|
|
actual ast.Type, expected ast.Type, n ast.Node) ast.Node {
|
|
|
|
if v.Implicit == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fromMap, ok := v.Implicit[actual]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
toFunc, ok := fromMap[expected]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ast.Call{
|
|
|
|
Func: toFunc,
|
|
|
|
Args: []ast.Node{n},
|
|
|
|
Posx: n.Pos(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 20:25:46 +01:00
|
|
|
func (v *TypeCheck) reset() {
|
2015-01-15 06:18:22 +01:00
|
|
|
v.Stack = nil
|
2015-01-12 08:38:21 +01:00
|
|
|
v.err = nil
|
|
|
|
}
|
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
func (v *TypeCheck) StackPush(t ast.Type) {
|
|
|
|
v.Stack = append(v.Stack, t)
|
2015-01-12 08:38:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 06:18:22 +01:00
|
|
|
func (v *TypeCheck) StackPop() ast.Type {
|
2015-01-12 08:38:21 +01:00
|
|
|
var x ast.Type
|
2015-01-15 06:18:22 +01:00
|
|
|
x, v.Stack = v.Stack[len(v.Stack)-1], v.Stack[:len(v.Stack)-1]
|
2015-01-12 08:38:21 +01:00
|
|
|
return x
|
|
|
|
}
|