Search in sources :

Example 11 with RuleEnvironment

use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.

the class ComposedMemberInfo method handleFParameters.

private void handleFParameters(TMember member, RuleEnvironment G) {
    EList<TFormalParameter> fpars = null;
    if (member instanceof TMethod) {
        TMethod method = (TMethod) member;
        fpars = method.getFpars();
    }
    if (member instanceof TSetter) {
        TSetter setter = (TSetter) member;
        fpars = new BasicEList<>();
        fpars.add(setter.getFpar());
    }
    if (fpars != null) {
        for (int i = 0; i < fpars.size(); i++) {
            TFormalParameter fpar = fpars.get(i);
            if (fParameters.size() <= i) {
                fParameters.add(new ComposedFParInfo());
            }
            ComposedFParInfo fpAggr = fParameters.get(i);
            Pair<TFormalParameter, RuleEnvironment> fpPair = new Pair<>(fpar, G);
            fpAggr.fpSiblings.add(fpPair);
        }
    }
}
Also used : TFormalParameter(org.eclipse.n4js.ts.types.TFormalParameter) TSetter(org.eclipse.n4js.ts.types.TSetter) TMethod(org.eclipse.n4js.ts.types.TMethod) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 12 with RuleEnvironment

use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.

the class TypeXpectMethod method getTypeString.

private String getTypeString(IEObjectCoveringRegion offset, boolean expectedType) {
    final String calculatedString;
    EObject eobject = offset.getEObject();
    if (eobject instanceof LiteralOrComputedPropertyName) {
        eobject = eobject.eContainer();
    }
    RuleEnvironment G = newRuleEnvironment(eobject);
    Result<org.eclipse.n4js.ts.typeRefs.TypeRef> result;
    if (expectedType) {
        if (!(eobject instanceof Expression && eobject.eContainer() != null))
            return "Not an Expression at given region (required to obtain expected type); got instead: " + eobject.eClass().getName();
        result = ts.expectedTypeIn(G, eobject.eContainer(), (Expression) eobject);
    } else {
        if (eobject instanceof BindingProperty) {
            /*-
				 * Small tweak to allow testing the inferred type of variable declarations within binding patterns. For
				 * example, without this tweak, the following test would fail with a "Not a TypableElement at given
				 * region" exception:
				 *
				 * // Xpect type of 'len' --> number
				 * var {length:len} = "hello";
				 */
            if (((BindingProperty) eobject).getValue() != null && ((BindingProperty) eobject).getValue().getVarDecl() != null) {
                eobject = ((BindingProperty) eobject).getValue().getVarDecl();
            }
        }
        if (!(eobject instanceof TypableElement))
            return "Not a TypableElement at given region; got instead: " + eobject.eClass().getName();
        result = ts.type(G, (TypableElement) eobject);
    }
    if (result.getRuleFailedException() != null) {
        calculatedString = result.getRuleFailedException().getMessage();
    } else {
        calculatedString = result.getValue().getTypeRefAsString();
    }
    return calculatedString;
}
Also used : Expression(org.eclipse.n4js.n4JS.Expression) ParameterizedCallExpression(org.eclipse.n4js.n4JS.ParameterizedCallExpression) TypableElement(org.eclipse.n4js.ts.types.TypableElement) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) EObject(org.eclipse.emf.ecore.EObject) LiteralOrComputedPropertyName(org.eclipse.n4js.n4JS.LiteralOrComputedPropertyName) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) RuleEnvironmentExtensions.newRuleEnvironment(org.eclipse.n4js.typesystem.RuleEnvironmentExtensions.newRuleEnvironment) BindingProperty(org.eclipse.n4js.n4JS.BindingProperty)

Example 13 with RuleEnvironment

use of org.eclipse.xsemantics.runtime.RuleEnvironment 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)

Example 14 with RuleEnvironment

use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.

the class MemberVisibilityChecker method getActualDeclaredReceiverType.

/**
 * Returns the actual receiver type, which usually simply is the declared type of the receiver type. However, in
 * case of classifier references, enums, or structural type references, the actual receiver may be differently
 * computed.
 */
private Type getActualDeclaredReceiverType(EObject context, TypeRef receiverType, ResourceSet resourceSet) {
    if (receiverType instanceof TypeTypeRef) {
        final RuleEnvironment G = RuleEnvironmentExtensions.newRuleEnvironment(context);
        return tsh.getStaticType(G, (TypeTypeRef) receiverType);
    }
    if (receiverType instanceof ThisTypeRef) {
        ThisTypeRef thisTypeRef = (ThisTypeRef) receiverType;
        if (thisTypeRef.isUseSiteStructuralTyping()) {
            FunctionOrFieldAccessor foa = N4JSASTUtils.getContainingFunctionOrAccessor(thisTypeRef);
            N4ClassifierDefinition classifier = EcoreUtil2.getContainerOfType(foa, N4ClassifierDefinition.class);
            return classifier.getDefinedType();
        }
    }
    if (receiverType instanceof FunctionTypeExpression) {
        if (resourceSet == null)
            return null;
        // Change receiverType to implicit super class Function.
        BuiltInTypeScope builtInTypeScope = BuiltInTypeScope.get(resourceSet);
        TObjectPrototype functionType = builtInTypeScope.getFunctionType();
        return functionType;
    }
    return receiverType.getDeclaredType();
}
Also used : ThisTypeRef(org.eclipse.n4js.ts.typeRefs.ThisTypeRef) FunctionTypeExpression(org.eclipse.n4js.ts.typeRefs.FunctionTypeExpression) FunctionOrFieldAccessor(org.eclipse.n4js.n4JS.FunctionOrFieldAccessor) TObjectPrototype(org.eclipse.n4js.ts.types.TObjectPrototype) N4ClassifierDefinition(org.eclipse.n4js.n4JS.N4ClassifierDefinition) TypeTypeRef(org.eclipse.n4js.ts.typeRefs.TypeTypeRef) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) BuiltInTypeScope(org.eclipse.n4js.ts.scoping.builtin.BuiltInTypeScope)

Example 15 with RuleEnvironment

use of org.eclipse.xsemantics.runtime.RuleEnvironment in project n4js by eclipse.

the class ComposedMemberInfo method initMemberAggregate.

// ///////////////////////// Init Methods ///////////////////////////
private synchronized void initMemberAggregate() {
    if (isInitialized)
        return;
    this.isSiblingMissing = siblings.contains(null);
    MemberType lastMType = null;
    for (Pair<TMember, RuleEnvironment> pair : siblings) {
        if (pair == null)
            continue;
        this.isEmpty = false;
        TMember member = pair.getKey();
        RuleEnvironment G = pair.getValue();
        lastMType = handleMemberTypes(lastMType, member);
        handleReadOnlyField(member);
        handleAccessibility(member);
        handleTypeRefLists(member, G);
        handleFParameters(member, G);
    }
    // init: fParameters
    List<TypeRef> currVariadicAccumulated = new LinkedList<>();
    for (ComposedFParInfo fpAggr : fParameters) {
        initFParAggregate(fpAggr);
        // handle: typeRefsVariadicAccumulated
        currVariadicAccumulated.addAll(fpAggr.typeRefsVariadic);
        fpAggr.typeRefsVariadicAccumulated.addAll(currVariadicAccumulated);
    }
    handleIsVariadicButLastFParIsDifferent();
    handleValidationProblems();
    this.isInitialized = true;
}
Also used : MemberType(org.eclipse.n4js.ts.types.MemberType) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) UnknownTypeRef(org.eclipse.n4js.ts.typeRefs.UnknownTypeRef) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) TMember(org.eclipse.n4js.ts.types.TMember) LinkedList(java.util.LinkedList)

Aggregations

RuleEnvironment (org.eclipse.xsemantics.runtime.RuleEnvironment)31 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)22 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)14 ComposedTypeRef (org.eclipse.n4js.ts.typeRefs.ComposedTypeRef)12 TypeArgument (org.eclipse.n4js.ts.typeRefs.TypeArgument)12 TypeTypeRef (org.eclipse.n4js.ts.typeRefs.TypeTypeRef)12 UnknownTypeRef (org.eclipse.n4js.ts.typeRefs.UnknownTypeRef)12 ExistentialTypeRef (org.eclipse.n4js.ts.typeRefs.ExistentialTypeRef)11 EObject (org.eclipse.emf.ecore.EObject)10 ThisTypeRef (org.eclipse.n4js.ts.typeRefs.ThisTypeRef)10 Result (org.eclipse.xsemantics.runtime.Result)10 BaseTypeRef (org.eclipse.n4js.ts.typeRefs.BaseTypeRef)9 BoundThisTypeRef (org.eclipse.n4js.ts.typeRefs.BoundThisTypeRef)9 FunctionTypeRef (org.eclipse.n4js.ts.typeRefs.FunctionTypeRef)9 StaticBaseTypeRef (org.eclipse.n4js.ts.typeRefs.StaticBaseTypeRef)9 StructuralTypeRef (org.eclipse.n4js.ts.typeRefs.StructuralTypeRef)9 ContainerType (org.eclipse.n4js.ts.types.ContainerType)9 Type (org.eclipse.n4js.ts.types.Type)9 StructuralTypingResult (org.eclipse.n4js.typesystem.StructuralTypingResult)9 PrimitiveType (org.eclipse.n4js.ts.types.PrimitiveType)8