Search in sources :

Example 1 with RuleEnvironmentExtensions.newRuleEnvironment

use of org.eclipse.n4js.typesystem.RuleEnvironmentExtensions.newRuleEnvironment in project n4js by eclipse.

the class TypeXpectMethod method getTypeArgumentsString.

private String getTypeArgumentsString(IEObjectCoveringRegion offset) {
    final EObject eobject = offset != null ? offset.getEObject() : null;
    final EObject container = eobject != null ? eobject.eContainer() : null;
    if (eobject == null || !(container instanceof ParameterizedCallExpression && ((ParameterizedCallExpression) container).getTarget() == eobject)) {
        // missing or invalid offset
        return "xpect method error: offset not given or does not point to target of a call expression";
    }
    if (!(eobject.eResource() instanceof N4JSResource)) {
        return "xpect method error: offset does not point to an EObject contained in a N4JSResource";
    }
    // offset points to the target of a call expression
    final ParameterizedCallExpression callExpr = (ParameterizedCallExpression) container;
    final RuleEnvironment G = RuleEnvironmentExtensions.newRuleEnvironment(eobject);
    final Result<TypeRef> targetTypeRef = ts.type(G, callExpr.getTarget());
    if (targetTypeRef.failed() || !(targetTypeRef.getValue() instanceof FunctionTypeExprOrRef)) {
        return "xpect method error: cannot infer type of call expression target OR it's not a FunctionTypeExprOrRef";
    }
    final List<TypeVariable> typeParams = ((FunctionTypeExprOrRef) targetTypeRef.getValue()).getTypeVars();
    // not interested in the actual typeParams, just the size
    final int expectedNumOfTypeArgs = typeParams.size();
    final List<TypeRef> typeArgs;
    if (callExpr.getTypeArgs().isEmpty()) {
        // no type arguments given in call expression -> use inferred type arguments
        // (should be the standard case when testing)
        final List<TypeRef> inferredTypeArgs = ASTMetaInfoUtils.getInferredTypeArgs(callExpr);
        if (inferredTypeArgs != null) {
            typeArgs = inferredTypeArgs;
        } else {
            typeArgs = Collections.emptyList();
        }
    } else {
        // call expression is parameterized -> use the explicitly given type arguments
        // (just provided for completeness)
        typeArgs = callExpr.getTypeArgs();
    }
    final StringBuilder sb = new StringBuilder();
    for (int i = 0; i < expectedNumOfTypeArgs; i++) {
        final TypeRef inferredTypeArg = i < typeArgs.size() ? typeArgs.get(i) : null;
        if (sb.length() > 0)
            sb.append(", ");
        if (inferredTypeArg != null)
            sb.append(inferredTypeArg.getTypeRefAsString());
        else
            sb.append("*missing*");
    }
    return sb.toString();
}
Also used : TypeVariable(org.eclipse.n4js.ts.types.TypeVariable) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) EObject(org.eclipse.emf.ecore.EObject) ParameterizedCallExpression(org.eclipse.n4js.n4JS.ParameterizedCallExpression) N4JSResource(org.eclipse.n4js.resource.N4JSResource) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) RuleEnvironmentExtensions.newRuleEnvironment(org.eclipse.n4js.typesystem.RuleEnvironmentExtensions.newRuleEnvironment) FunctionTypeExprOrRef(org.eclipse.n4js.ts.typeRefs.FunctionTypeExprOrRef)

Aggregations

EObject (org.eclipse.emf.ecore.EObject)1 ParameterizedCallExpression (org.eclipse.n4js.n4JS.ParameterizedCallExpression)1 N4JSResource (org.eclipse.n4js.resource.N4JSResource)1 FunctionTypeExprOrRef (org.eclipse.n4js.ts.typeRefs.FunctionTypeExprOrRef)1 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)1 TypeVariable (org.eclipse.n4js.ts.types.TypeVariable)1 RuleEnvironmentExtensions.newRuleEnvironment (org.eclipse.n4js.typesystem.RuleEnvironmentExtensions.newRuleEnvironment)1 RuleEnvironment (org.eclipse.xsemantics.runtime.RuleEnvironment)1