2015-01-13 20:24:42 +01:00
|
|
|
package lang
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config/lang/ast"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IdentifierCheck is a SemanticCheck that checks that all identifiers
|
|
|
|
// resolve properly and that the right number of arguments are passed
|
|
|
|
// to functions.
|
|
|
|
type IdentifierCheck struct {
|
|
|
|
Scope *Scope
|
|
|
|
|
|
|
|
err error
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *IdentifierCheck) Visit(root ast.Node) error {
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
defer c.reset()
|
|
|
|
root.Accept(c.visit)
|
|
|
|
return c.err
|
|
|
|
}
|
|
|
|
|
2015-01-15 01:36:01 +01:00
|
|
|
func (c *IdentifierCheck) visit(raw ast.Node) ast.Node {
|
2015-01-13 20:24:42 +01:00
|
|
|
if c.err != nil {
|
2015-01-15 01:36:01 +01:00
|
|
|
return raw
|
2015-01-13 20:24:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch n := raw.(type) {
|
|
|
|
case *ast.Call:
|
|
|
|
c.visitCall(n)
|
|
|
|
case *ast.VariableAccess:
|
|
|
|
c.visitVariableAccess(n)
|
|
|
|
case *ast.Concat:
|
|
|
|
// Ignore
|
|
|
|
case *ast.LiteralNode:
|
|
|
|
// Ignore
|
|
|
|
default:
|
|
|
|
c.createErr(n, fmt.Sprintf("unknown node: %#v", raw))
|
|
|
|
}
|
2015-01-15 01:36:01 +01:00
|
|
|
|
|
|
|
// We never do replacement with this visitor
|
|
|
|
return raw
|
2015-01-13 20:24:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *IdentifierCheck) visitCall(n *ast.Call) {
|
|
|
|
// Look up the function in the map
|
|
|
|
function, ok := c.Scope.LookupFunc(n.Func)
|
|
|
|
if !ok {
|
|
|
|
c.createErr(n, fmt.Sprintf("unknown function called: %s", n.Func))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-13 21:40:47 +01:00
|
|
|
// Break up the args into what is variadic and what is required
|
|
|
|
args := n.Args
|
|
|
|
if function.Variadic && len(args) > len(function.ArgTypes) {
|
|
|
|
args = n.Args[:len(function.ArgTypes)]
|
|
|
|
}
|
|
|
|
|
2015-01-13 20:24:42 +01:00
|
|
|
// Verify the number of arguments
|
2015-01-13 21:40:47 +01:00
|
|
|
if len(args) != len(function.ArgTypes) {
|
2015-01-13 20:24:42 +01:00
|
|
|
c.createErr(n, fmt.Sprintf(
|
|
|
|
"%s: expected %d arguments, got %d",
|
|
|
|
n.Func, len(function.ArgTypes), len(n.Args)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *IdentifierCheck) visitVariableAccess(n *ast.VariableAccess) {
|
|
|
|
// Look up the variable in the map
|
|
|
|
if _, ok := c.Scope.LookupVar(n.Name); !ok {
|
|
|
|
c.createErr(n, fmt.Sprintf(
|
|
|
|
"unknown variable accessed: %s", n.Name))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *IdentifierCheck) createErr(n ast.Node, str string) {
|
|
|
|
c.err = fmt.Errorf("%s: %s", n.Pos(), str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *IdentifierCheck) reset() {
|
|
|
|
c.err = nil
|
|
|
|
}
|