Search in sources :

Example 1 with Kind

use of sharpen.xobotos.api.interop.NativeMethod.Kind in project XobotOS by xamarin.

the class NativeGlueGenerator method generate.

public Method generate() {
    final Method method = new Method(_nativeFunction, _returnType, Visibility.PUBLIC, Flags.EXPORT);
    final List<Expression> args = new ArrayList<Expression>();
    final List<Statement> preStmnts = new ArrayList<Statement>();
    final List<Statement> postStmnts = new ArrayList<Statement>();
    final Kind kind = _method.getKind();
    final ByRef<Expression> instanceArg;
    if ((kind == Kind.INSTANCE) || (kind == Kind.PROXY))
        instanceArg = new ByRef<Expression>(null);
    else
        instanceArg = null;
    if (_implicitInstance != null) {
        Parameter param = new Parameter(new PointerType(_nativeClass), "_instance");
        method.addParameter(param);
        Expression arg = new VariableReference(param);
        if (instanceArg != null)
            instanceArg.value = arg;
        else
            args.add(new DereferenceExpression(arg));
    }
    for (int i = 0; i < _paramInfo.length; i++) {
        if (_paramInfo[i] == null)
            continue;
        VariableDeclaration vdecl = (VariableDeclaration) _decl.parameters().get(i);
        IVariableBinding vbinding = vdecl.resolveBinding();
        final String name = getNativeName(vbinding.getName());
        boolean hasInstanceArg = (instanceArg != null) && (instanceArg.value == null);
        final boolean isInstanceArg = hasInstanceArg && (i == 0);
        INativeMarshalContext context = new INativeMarshalContext() {

            @Override
            public void addPreStatement(Statement statement) {
                preStmnts.add(statement);
            }

            @Override
            public void addPostStatement(Statement statement) {
                postStmnts.add(statement);
            }

            @Override
            public NativeVariable createParameter(String suffix, AbstractTypeReference type) {
                String pname = suffix != null ? name + "_" + suffix : name;
                Parameter param = new Parameter(type, pname);
                method.addParameter(param);
                return new NativeVariable(param);
            }

            @Override
            public NativeVariable createVariable(String suffix, AbstractTypeReference type, Expression init) {
                String vname = suffix != null ? name + "_" + suffix : name;
                LocalVariable var = new LocalVariable(type, vname, init);
                preStmnts.add(var);
                return new NativeVariable(var);
            }

            @Override
            public void addArgument(Expression arg) {
                if (isInstanceArg)
                    instanceArg.value = arg;
                else
                    args.add(arg);
            }
        };
        _paramInfo[i].marshal.marshalNative(context, _paramInfo[i].mode, _paramInfo[i].flags);
    }
    if (_method.getKind() == Kind.DESTRUCTOR) {
        method.getBody().addStatement(new DestructorInvocation(args.get(0)));
        return method;
    }
    final AbstractInvocation invocation;
    if (_method.getKind() == Kind.INSTANCE) {
        Expression member = new InstanceMemberAccess(instanceArg.value, _nativeName);
        invocation = new MethodInvocation(member, args);
    } else if (_method.getKind() == Kind.PROXY) {
        Expression member = new StaticMemberAccess(_nativeClass, _nativeName);
        args.add(0, new DereferenceExpression(instanceArg.value));
        invocation = new MethodInvocation(member, args);
    } else if (_method.getKind() == Kind.STATIC) {
        Expression member = new StaticMemberAccess(_nativeClass, _nativeName);
        invocation = new MethodInvocation(member, args);
    } else if (_method.getKind() == Kind.CONSTRUCTOR) {
        AbstractTypeReference type = ((PointerType) _returnType).getElementType();
        invocation = new ConstructorInvocation(type, args);
    } else {
        return null;
    }
    for (Statement stm : preStmnts) {
        method.getBody().addStatement(stm);
    }
    final NativeVariable retval;
    if (_returnHelper != null) {
        NativeVariable cppRetval = NativeVariable.createLocal(method.getBody(), _returnHelper.getRealNativeType(), "_returnObj", invocation, NativeVariable.Flags.BYREF);
        retval = NativeVariable.createLocal(method.getBody(), _returnHelper.getNativePInvokeType(), "_retval", _returnHelper.marshalNativeRetval(cppRetval.getReference()), NativeVariable.Flags.BYREF);
        method.getBody().addStatement(new DestructorInvocation(cppRetval.getReference()));
    } else if (_returnInfo != null) {
        retval = NativeVariable.createLocal(method.getBody(), _returnType, "_retval", invocation);
    } else {
        method.getBody().addStatement(invocation);
        retval = null;
    }
    for (Statement stm : postStmnts) {
        method.getBody().addStatement(stm);
    }
    if (retval != null) {
        method.getBody().addStatement(new ReturnStatement(retval.getReference()));
    }
    return method;
}
Also used : ByRef(sharpen.core.framework.ByRef) ArrayList(java.util.ArrayList) Kind(sharpen.xobotos.api.interop.NativeMethod.Kind) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 2 with Kind

use of sharpen.xobotos.api.interop.NativeMethod.Kind in project XobotOS by xamarin.

the class NativeMethodBuilder method resolve.

public boolean resolve(IMarshalContext context) {
    if (_resolveFailed)
        return false;
    if (_resolved)
        return true;
    _binding = _node.resolveBinding();
    ITypeBinding returnType = _binding.getReturnType();
    ITypeBinding[] paramTypes = _binding.getParameterTypes();
    _methodName = BindingUtils.qualifiedSignature(_binding);
    final Signature signature = getNativeMethod().getSignature();
    final Kind kind = _native.getKind();
    MarshalInfo returnInfo = null;
    if (isVoid(returnType) && (kind == Kind.CONSTRUCTOR)) {
        ReturnInfo info = signature.getReturnInfo();
        if ((info != null) && (info.marshal != null))
            returnInfo = info.marshal.resolve(null);
        if (returnInfo == null) {
            Sharpen.Log(Level.SEVERE, "Missing marshal info for return type of constructor '%s'", _methodName);
            _resolveFailed = true;
        }
    } else if (!isVoid(returnType) && !getNativeMethod().returnVoid()) {
        ReturnInfo info = signature.getReturnInfo();
        if (info != null) {
            if (info.marshal != null)
                returnInfo = info.marshal.resolve(returnType);
            else
                returnInfo = context.getMarshalInfo(returnType);
        } else {
            returnInfo = context.getMarshalInfo(returnType);
        }
        if (returnInfo == null) {
            Sharpen.Log(Level.SEVERE, "Missing marshal info for return type '%s' of method '%s'", BindingUtils.qualifiedName(returnType), _methodName);
            _resolveFailed = true;
        }
    }
    if (kind == Kind.CONSTRUCTOR) {
        if (returnInfo == null) {
            Sharpen.Log(Level.SEVERE, "Missing marshal info for return type '%s' of constructor '%s'", BindingUtils.qualifiedName(returnType), _methodName);
            _resolveFailed = true;
        } else if (!(returnInfo instanceof MarshalAsClass)) {
            Sharpen.Log(Level.SEVERE, "Return type '%s' of constructor '%s' must have MarshalAsClass marshal info", BindingUtils.qualifiedName(returnType), _methodName);
            _resolveFailed = true;
        }
    }
    if (kind == Kind.DESTRUCTOR) {
        if (_returnInfo != null) {
            Sharpen.Log(Level.SEVERE, "Destructor '%s' cannot return a value.", _methodName);
            _resolveFailed = true;
        } else if (paramTypes.length != 1) {
            Sharpen.Log(Level.SEVERE, "Destructor '%s' must take one single argument.", _methodName);
            _resolveFailed = true;
        } else if (_nativeHandle == null) {
            Sharpen.Log(Level.SEVERE, "Destructor '%s' must have a native handle", _methodName);
            _resolveFailed = true;
        }
    }
    if (_resolveFailed)
        return false;
    if (returnInfo != null) {
        if (!returnInfo.resolve(context, returnType)) {
            Sharpen.Log(Level.SEVERE, "Cannot resolve return value in '%s'.", _methodName);
            _resolveFailed = true;
        } else {
            _returnInfo = new ElementInfo(returnType, returnInfo, Mode.OUT, null);
        }
    }
    if (kind == Kind.DESTRUCTOR) {
        _paramInfo = new ElementInfo[1];
        if (_nativeHandle == null) {
            Sharpen.Log(Level.SEVERE, "Missing 'native-handle' for destructor '%s'.", _methodName);
            _resolveFailed = true;
        } else {
            MarshalInfo info = new MarshalAsClass(paramTypes[0], null, _nativeHandle.getTemplate());
            VariableDeclaration vdecl = (VariableDeclaration) _node.parameters().get(0);
            IVariableBinding vbinding = vdecl.resolveBinding();
            ITypeBinding vtype = vbinding.getType();
            if (!info.resolve(context, vtype)) {
                Sharpen.Log(Level.SEVERE, "Cannot resolve marshal info for destructor '%s'.", _methodName);
                _resolveFailed = true;
            } else {
                _paramInfo[0] = new ElementInfo(vtype, info, null, null);
            }
        }
    } else {
        _paramInfo = new ElementInfo[paramTypes.length];
        for (int i = 0; i < paramTypes.length; i++) {
            final ParameterInfo info = signature.getParameterInfo(i);
            if ((info == null) || (info.mode == Mode.REMOVE))
                continue;
            MarshalInfo marshal;
            if (info.marshal != null)
                marshal = info.marshal.resolve(paramTypes[i]);
            else
                marshal = context.getMarshalInfo(paramTypes[i]);
            if (marshal == null) {
                Sharpen.Log(Level.SEVERE, "Missing marshal info for type '%s' in method '%s'", BindingUtils.qualifiedName(paramTypes[i]), _methodName);
                _resolveFailed = true;
                continue;
            }
            VariableDeclaration vdecl = (VariableDeclaration) _node.parameters().get(i);
            IVariableBinding vbinding = vdecl.resolveBinding();
            ITypeBinding vtype = vbinding.getType();
            boolean isInstanceArg;
            if ((kind == Kind.INSTANCE) || (kind == Kind.PROXY))
                isInstanceArg = (i == 0) && !signature.implicitInstance();
            else
                isInstanceArg = false;
            if (isInstanceArg) {
                _paramInfo[0] = new ElementInfo(vtype, marshal, Mode.REF, null);
                if (!marshal.resolve(context, vtype)) {
                    Sharpen.Log(Level.SEVERE, "Cannot resolve instance parameter of method '%s'.", _methodName);
                    _resolveFailed = true;
                }
            } else {
                _paramInfo[i] = new ElementInfo(vtype, marshal, info.mode, info.flags);
                if (!marshal.resolve(context, vtype)) {
                    Sharpen.Log(Level.SEVERE, "Cannot resolve parameter %d of method '%s'.", i, _methodName);
                    _resolveFailed = true;
                }
            }
        }
    }
    if (signature.implicitInstance()) {
        if (_nativeHandle == null) {
            Sharpen.Log(Level.SEVERE, "Cannot use <add-instance> without <native-handle> in '%s'", _methodName);
            _resolveFailed = true;
            return false;
        }
        String fieldName = _nativeHandle.getTemplate().getField();
        _implicitInstance = findField(_binding, fieldName);
        if (_implicitInstance == null) {
            Sharpen.Log(Level.SEVERE, "No such field '%s' in type '%s'", fieldName, BindingUtils.qualifiedName(_binding.getDeclaringClass()));
            _resolveFailed = true;
            return false;
        }
    }
    if (_resolveFailed)
        return false;
    _resolved = true;
    return true;
}
Also used : MarshalAsClass(sharpen.xobotos.api.interop.marshal.MarshalAsClass) ParameterInfo(sharpen.xobotos.api.interop.Signature.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) MarshalInfo(sharpen.xobotos.api.interop.marshal.MarshalInfo) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Kind(sharpen.xobotos.api.interop.NativeMethod.Kind) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) ReturnInfo(sharpen.xobotos.api.interop.Signature.ReturnInfo)

Aggregations

IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)2 VariableDeclaration (org.eclipse.jdt.core.dom.VariableDeclaration)2 Kind (sharpen.xobotos.api.interop.NativeMethod.Kind)2 ArrayList (java.util.ArrayList)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 ByRef (sharpen.core.framework.ByRef)1 ParameterInfo (sharpen.xobotos.api.interop.Signature.ParameterInfo)1 ReturnInfo (sharpen.xobotos.api.interop.Signature.ReturnInfo)1 MarshalAsClass (sharpen.xobotos.api.interop.marshal.MarshalAsClass)1 MarshalInfo (sharpen.xobotos.api.interop.marshal.MarshalInfo)1