private void defineVariable(VariableRef ref, Type type) {
            Variable v = ref.getVariable();
            boolean staticallyTyped = v != null ? v.isStaticallyTyped() : false;

            Variable newVar = null;
            if (v == null) {
                newVar = new Variable(ref.getSourceInfo(), ref.getName(), type,
                                      staticallyTyped);
            }
            else {
                newVar = new Variable(ref.getSourceInfo(), ref.getName(),
                                      v.getTypeName(), staticallyTyped);
                newVar.setType(type);
            }

            mScope.declareVariable(newVar);
            if (!mScope.bindToVariable(ref)) {
                // Shouldn't happen.
                error("variable.undefined", ref.getName(), ref);
            }
        }