Search in sources :

Example 6 with ContainerType

use of org.eclipse.n4js.ts.types.ContainerType in project n4js by eclipse.

the class N4JSASTUtils method isSemiLegalAssignmentToFinalFieldInCtor.

/**
 * Returns true iff <code>expr</code> is a semi-legal assignment expression within a constructor to a final field of
 * the same class. Here, "semi"-legal means that one requirement for a fully legal such assignment is <b>not</b>
 * checked by this method: the requirement that the declaration of the assigned final field must not have an
 * initializer expression. For a fully legal assignment to a final field this has to be checked by client code.
 */
public static boolean isSemiLegalAssignmentToFinalFieldInCtor(EObject expr, TMember writtenMember) {
    if (!(expr instanceof AssignmentExpression))
        return false;
    final AssignmentExpression assExpr = (AssignmentExpression) expr;
    // left-hand side must be a property access to 'this'
    final Expression lhs = assExpr.getLhs();
    if (!(lhs instanceof ParameterizedPropertyAccessExpression))
        return false;
    final ParameterizedPropertyAccessExpression paExpr = (ParameterizedPropertyAccessExpression) lhs;
    if (!(paExpr.getTarget() instanceof ThisLiteral))
        return false;
    // BUT: check this only if we have a resolved property in paExpr (important if this method used from scoping)
    if (paExpr.getProperty() != null && !paExpr.getProperty().eIsProxy()) {
        if (writtenMember != null) {
            // case 1: writtenMember provided as argument -> must be identical
            if (paExpr.getProperty() != writtenMember)
                return false;
        } else {
            // case 2: writtenMember not provided -> take from paExpr
            if (paExpr.getProperty() instanceof TMember)
                writtenMember = (TMember) paExpr.getProperty();
        }
    }
    // written member must be a final field
    if (!(writtenMember instanceof TField))
        return false;
    final TField field = (TField) writtenMember;
    if (!field.isFinal())
        return false;
    // (IMPORTANT: we do not assert !field.isHasExpression() here, which would be required for a fully-legal write
    // access)
    // assExpr must be located in the constructor of the owner of 'field'
    // (a) find type model element for the function containing the assignment expression
    final FunctionDefinition containingFunction = getContainingFunction(assExpr);
    if (containingFunction == null)
        return false;
    final Type containingTFunction = containingFunction.getDefinedType();
    if (containingTFunction == null)
        return false;
    // (b) find constructor of the owner of the field
    final ContainerType<?> ownerOfField = field.getContainingType();
    if (ownerOfField == null)
        return false;
    final TMember ctorOfOwner = getOwnOrSuperCtor(ownerOfField);
    if (ctorOfOwner == null)
        return false;
    // (c) compare
    boolean simpleCompare = containingTFunction == ctorOfOwner;
    if (simpleCompare) {
        return true;
    }
    // filled type:
    if (containingTFunction.eContainer() instanceof TClass) {
        TClass containingTClass = (TClass) containingTFunction.eContainer();
        if (containingTClass.isStaticPolyfill() && containingTClass.getSuperClassRef() != null && containingTClass.getSuperClassRef().getDeclaredType() instanceof TClass) {
            // get replaced ctor:
            TClass filledClass = (TClass) containingTClass.getSuperClassRef().getDeclaredType();
            TMember replacedCtorOfFilledClass = getOwnOrSuperCtor(filledClass);
            // compare (incl. null)
            return replacedCtorOfFilledClass == ctorOfOwner;
        }
    }
    return false;
}
Also used : ContainerType(org.eclipse.n4js.ts.types.ContainerType) Type(org.eclipse.n4js.ts.types.Type) TField(org.eclipse.n4js.ts.types.TField) TMember(org.eclipse.n4js.ts.types.TMember) TClass(org.eclipse.n4js.ts.types.TClass)

Example 7 with ContainerType

use of org.eclipse.n4js.ts.types.ContainerType in project n4js by eclipse.

the class WildcardImpl method getDeclaredOrImplicitUpperBound.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public TypeRef getDeclaredOrImplicitUpperBound() {
    final TypeRef declUB = this.getDeclaredUpperBound();
    if ((declUB != null)) {
        return declUB;
    }
    TypeRef _declaredLowerBound = this.getDeclaredLowerBound();
    boolean _tripleNotEquals = (_declaredLowerBound != null);
    if (_tripleNotEquals) {
        return null;
    }
    final EObject parent = this.eContainer();
    if ((parent instanceof ParameterizedTypeRef)) {
        final int typeArgIndex = ((ParameterizedTypeRef) parent).getTypeArgs().indexOf(this);
        if ((typeArgIndex >= 0)) {
            final Object declType = ((ParameterizedTypeRef) parent).eGet(TypeRefsPackage.eINSTANCE.getParameterizedTypeRef_DeclaredType(), false);
            if ((declType instanceof ContainerType<?>)) {
                boolean _eIsProxy = ((ContainerType<?>) declType).eIsProxy();
                boolean _not = (!_eIsProxy);
                if (_not) {
                    final EList<TypeVariable> typeVars = ((ContainerType<?>) declType).getTypeVars();
                    TypeVariable _xifexpression = null;
                    int _size = typeVars.size();
                    boolean _lessThan = (typeArgIndex < _size);
                    if (_lessThan) {
                        _xifexpression = typeVars.get(typeArgIndex);
                    } else {
                        _xifexpression = null;
                    }
                    final TypeVariable typeVar = _xifexpression;
                    if ((typeVar != null)) {
                        final TypeRef implicitUB = typeVar.getDeclaredUpperBound();
                        return implicitUB;
                    }
                }
            }
        }
    }
    return null;
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) TypeVariable(org.eclipse.n4js.ts.types.TypeVariable) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) ContainerType(org.eclipse.n4js.ts.types.ContainerType) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject)

Example 8 with ContainerType

use of org.eclipse.n4js.ts.types.ContainerType in project n4js by eclipse.

the class TMemberImpl method getContainingType.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public ContainerType<?> getContainingType() {
    final EObject myContainer = this.eContainer();
    ContainerType<?> _xifexpression = null;
    if ((myContainer instanceof ContainerType<?>)) {
        _xifexpression = ((ContainerType<?>) myContainer);
    } else {
        _xifexpression = null;
    }
    return _xifexpression;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) ContainerType(org.eclipse.n4js.ts.types.ContainerType)

Example 9 with ContainerType

use of org.eclipse.n4js.ts.types.ContainerType in project n4js by eclipse.

the class TMethodImpl method getContainingType.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public ContainerType<?> getContainingType() {
    final EObject myContainer = this.eContainer();
    ContainerType<?> _xifexpression = null;
    if ((myContainer instanceof ContainerType<?>)) {
        _xifexpression = ((ContainerType<?>) myContainer);
    } else {
        _xifexpression = null;
    }
    return _xifexpression;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ContainerType(org.eclipse.n4js.ts.types.ContainerType)

Example 10 with ContainerType

use of org.eclipse.n4js.ts.types.ContainerType in project n4js by eclipse.

the class TFunctionImpl method isCallableConstructor.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public boolean isCallableConstructor() {
    final EObject parent = this.eContainer();
    boolean _xifexpression = false;
    if ((parent instanceof ContainerType<?>)) {
        TMethod _callableCtor = ((ContainerType<?>) parent).getCallableCtor();
        _xifexpression = (_callableCtor == this);
    } else {
        _xifexpression = false;
    }
    return _xifexpression;
}
Also used : TMethod(org.eclipse.n4js.ts.types.TMethod) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) ContainerType(org.eclipse.n4js.ts.types.ContainerType)

Aggregations

ContainerType (org.eclipse.n4js.ts.types.ContainerType)16 EObject (org.eclipse.emf.ecore.EObject)10 TMember (org.eclipse.n4js.ts.types.TMember)9 Type (org.eclipse.n4js.ts.types.Type)9 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)6 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)6 TMethod (org.eclipse.n4js.ts.types.TMethod)4 Result (org.eclipse.xsemantics.runtime.Result)4 RuleEnvironment (org.eclipse.xsemantics.runtime.RuleEnvironment)4 Optional (java.util.Optional)3 InternalEObject (org.eclipse.emf.ecore.InternalEObject)3 TClass (org.eclipse.n4js.ts.types.TClass)3 TypeVariable (org.eclipse.n4js.ts.types.TypeVariable)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 EcoreUtil (org.eclipse.emf.ecore.util.EcoreUtil)2 Expression (org.eclipse.n4js.n4JS.Expression)2 IndexedAccessExpression (org.eclipse.n4js.n4JS.IndexedAccessExpression)2 ParameterizedPropertyAccessExpression (org.eclipse.n4js.n4JS.ParameterizedPropertyAccessExpression)2 BaseTypeRef (org.eclipse.n4js.ts.typeRefs.BaseTypeRef)2