Search in sources :

Example 1 with JvmTypeParameterDeclarator

use of org.eclipse.xtext.common.types.JvmTypeParameterDeclarator in project xtext-xtend by eclipse.

the class XtendImportedNamespaceScopeProvider method getScope.

@Override
public IScope getScope(final EObject context, final EReference reference) {
    EClass referenceType = reference.getEReferenceType();
    if (TypesPackage.Literals.JVM_TYPE.isSuperTypeOf(referenceType)) {
        if (context instanceof XImportDeclaration) {
            Resource resource = context.eResource();
            IJvmTypeProvider typeProvider = typeScopeProvider.getTypeProvider(resource.getResourceSet());
            AbstractTypeScope typeScope = typeScopeProvider.createTypeScope(typeProvider, null);
            IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(context.eResource().getResourceSet());
            IResourceDescription resourceDescription = descriptions.getResourceDescription(resource.getURI());
            if (resourceDescription != null) {
                typeScope = new LocalResourceFilteringTypeScope(typeScope, resourceDescription);
            }
            RecordingTypeScope recordingTypeScope = new RecordingTypeScope(typeScope, getImportedNamesSet(resource), getQualifiedNameConverter());
            // TODO this scope doesn't support binary syntax for inner types. It should be a KnownTypes scope which doesn't allow simple names
            // Unfortunately I cannot use a RecordingTypeScope as a parent as it is not compatible...
            IScope scope = SelectableBasedScope.createScope(recordingTypeScope, getAllDescriptions(resource), reference.getEReferenceType(), false);
            return scope;
        }
        final XtendFile xtendFile = getXtendFile(context);
        final Resource resource = xtendFile.eResource();
        AbstractScope result = resourceScopeCache.get("type.scope", xtendFile.eResource(), new Provider<AbstractScope>() {

            @Override
            public AbstractScope get() {
                IJvmTypeProvider typeProvider = typeScopeProvider.getTypeProvider(resource.getResourceSet());
                AbstractTypeScope typeScope = typeScopeProvider.createTypeScope(typeProvider, null);
                IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(context.eResource().getResourceSet());
                IResourceDescription resourceDescription = descriptions.getResourceDescription(resource.getURI());
                if (resourceDescription != null) {
                    typeScope = new LocalResourceFilteringTypeScope(typeScope, resourceDescription);
                }
                RecordingTypeScope recordingTypeScope = new RecordingTypeScope(typeScope, getImportedNamesSet(resource), getQualifiedNameConverter());
                AbstractScope rootTypeScope = getRootTypeScope(xtendFile, recordingTypeScope);
                AbstractScope importScope = getImportScope(xtendFile.getImportSection(), rootTypeScope, recordingTypeScope);
                AbstractScope localTypes = getResourceTypeScope(xtendFile.eResource(), xtendFile.getPackage(), importScope);
                AbstractScope primitiveAware = new PrimitiveAwareScope(localTypes, typeScope);
                AbstractScope caching = new CachingTypeScope(primitiveAware);
                return caching;
            }
        });
        if (context instanceof AnonymousClass) {
            // necessary for the super type of an anonymous class expression
            JvmDeclaredType inferredAnonymousType = associations.getInferredType((AnonymousClass) context);
            if (inferredAnonymousType != null)
                result = new LocalTypeScope(singletonList(inferredAnonymousType), result);
        }
        XtendMember syntacticContainer = EcoreUtil2.getContainerOfType(context, XtendMember.class);
        if (syntacticContainer != null) {
            result = getContainerScope(syntacticContainer, result);
        }
        EObject logicalContainer = logicalContainerProvider.getNearestLogicalContainer(context);
        if (logicalContainer != null) {
            List<List<JvmTypeParameter>> typeParameters = new ArrayList<List<JvmTypeParameter>>();
            while (logicalContainer instanceof JvmTypeParameterDeclarator) {
                JvmTypeParameterDeclarator typeParamProvider = (JvmTypeParameterDeclarator) logicalContainer;
                if (!typeParamProvider.getTypeParameters().isEmpty()) {
                    typeParameters.add(typeParamProvider.getTypeParameters());
                }
                logicalContainer = logicalContainer.eContainer();
            }
            if (!typeParameters.isEmpty())
                result = new TypeParameterScope(typeParameters, result);
        }
        return result;
    } else if (TypesPackage.Literals.JVM_CONSTRUCTOR.isSuperTypeOf(referenceType)) {
        IScope typeScope = getScope(context, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE);
        // this is not called from the type resolution where we want to allow constructors to link to interfaces
        // in order to improve the error message, therefore we use a strict wrapper here
        IScope result = new ConstructorTypeScopeWrapper(context, IVisibilityHelper.ALL, typeScope, true);
        return result;
    } else {
        throw new IllegalArgumentException("Unexpected global request for " + reference);
    }
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) JvmTypeParameterDeclarator(org.eclipse.xtext.common.types.JvmTypeParameterDeclarator) AbstractTypeScope(org.eclipse.xtext.common.types.xtext.AbstractTypeScope) ArrayList(java.util.ArrayList) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) IJvmTypeProvider(org.eclipse.xtext.common.types.access.IJvmTypeProvider) EClass(org.eclipse.emf.ecore.EClass) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) Resource(org.eclipse.emf.ecore.resource.Resource) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) ConstructorTypeScopeWrapper(org.eclipse.xtext.xbase.scoping.batch.ConstructorTypeScopeWrapper)

Example 2 with JvmTypeParameterDeclarator

use of org.eclipse.xtext.common.types.JvmTypeParameterDeclarator in project xtext-xtend by eclipse.

the class XtendImportedNamespaceScopeProvider method getContainerScope.

private AbstractScope getContainerScope(XtendMember syntacticContainer, AbstractScope result) {
    JvmDeclaredType innermost = null;
    List<List<JvmTypeParameter>> typeParameters = null;
    XtendMember workWithMe = syntacticContainer;
    while (workWithMe != null) {
        EObject derivedJvmElement = getAssociations().getPrimaryJvmElement(workWithMe);
        // scope for JvmTypeParameterDeclarator
        if (derivedJvmElement instanceof JvmTypeParameterDeclarator) {
            JvmTypeParameterDeclarator parameterDeclarator = (JvmTypeParameterDeclarator) derivedJvmElement;
            List<JvmTypeParameter> current = parameterDeclarator.getTypeParameters();
            if (!current.isEmpty()) {
                if (typeParameters == null) {
                    typeParameters = Lists.newArrayListWithCapacity(3);
                }
                typeParameters.add(current);
            }
        }
        if (innermost == null && derivedJvmElement instanceof JvmDeclaredType) {
            if (syntacticContainer != workWithMe) {
                // prevent stackoverflow / cyclic resolution
                innermost = (JvmDeclaredType) derivedJvmElement;
            }
        }
        EObject container = workWithMe;
        do {
            container = container.eContainer();
            if (container == null) {
                if (typeParameters == null) {
                    if (innermost == null)
                        return result;
                    return new NestedTypesScope(innermost, this, result);
                }
                TypeParameterScope typeParameterScope = new TypeParameterScope(typeParameters, result);
                if (innermost == null) {
                    return typeParameterScope;
                }
                return new NestedTypesScope(innermost, this, typeParameterScope);
            }
        } while (!(container instanceof XtendMember));
        workWithMe = (XtendMember) container;
    }
    return result;
}
Also used : XtendMember(org.eclipse.xtend.core.xtend.XtendMember) JvmTypeParameterDeclarator(org.eclipse.xtext.common.types.JvmTypeParameterDeclarator) EObject(org.eclipse.emf.ecore.EObject) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList)

Example 3 with JvmTypeParameterDeclarator

use of org.eclipse.xtext.common.types.JvmTypeParameterDeclarator in project xtext-xtend by eclipse.

the class XtendGenerator method compileLocalTypeStubs.

public void compileLocalTypeStubs(final JvmFeature feature, final ITreeAppendable appendable, final GeneratorConfig config) {
    final Function1<JvmGenericType, Boolean> _function = (JvmGenericType it) -> {
        boolean _isAnonymous = it.isAnonymous();
        return Boolean.valueOf((!_isAnonymous));
    };
    final Consumer<JvmGenericType> _function_1 = (JvmGenericType it) -> {
        appendable.newLine();
        EObject _head = IterableExtensions.<EObject>head(this.getSourceElements(it));
        final AnonymousClass anonymousClass = ((AnonymousClass) _head);
        final ITreeAppendable childAppendable = appendable.trace(anonymousClass);
        childAppendable.append("abstract class ");
        this._treeAppendableUtil.traceSignificant(childAppendable, anonymousClass).append(it.getSimpleName());
        boolean _isEmpty = it.getTypeParameters().isEmpty();
        if (_isEmpty) {
            childAppendable.append(" ");
        }
        this.generateExtendsClause(it, childAppendable, null);
        childAppendable.append("{").increaseIndentation();
        boolean _needSyntheticThisVariable = this.needSyntheticThisVariable(anonymousClass, it);
        if (_needSyntheticThisVariable) {
            Pair<String, JvmGenericType> _mappedTo = Pair.<String, JvmGenericType>of("this", it);
            String _simpleName = it.getSimpleName();
            String _plus = ("_this" + _simpleName);
            final String thisName = childAppendable.declareSyntheticVariable(_mappedTo, _plus);
            childAppendable.newLine().append("final ").append(it.getSimpleName()).append(" ").append(thisName).append(" = this;").newLine();
        }
        final Procedure1<LoopParams> _function_2 = (LoopParams it_1) -> {
            final Function1<ITreeAppendable, ITreeAppendable> _function_3 = (ITreeAppendable it_2) -> {
                return it_2.newLine();
            };
            it_1.setSeparator(_function_3);
        };
        final Procedure1<JvmMember> _function_3 = (JvmMember it_1) -> {
            final ITreeAppendable memberAppendable = this._treeAppendableUtil.traceWithComments(childAppendable, it_1);
            memberAppendable.openScope();
            if ((it_1 instanceof JvmOperation)) {
                final ITreeAppendable tracedAppendable = childAppendable.trace(it_1);
                tracedAppendable.newLine();
                this.generateJavaDoc(it_1, tracedAppendable, config);
                this.generateVisibilityModifier(it_1, tracedAppendable);
                tracedAppendable.append("abstract ");
                this.generateTypeParameterDeclaration(((JvmTypeParameterDeclarator) it_1), tracedAppendable, null);
                JvmTypeReference _returnType = ((JvmOperation) it_1).getReturnType();
                boolean _tripleEquals = (_returnType == null);
                if (_tripleEquals) {
                    tracedAppendable.append("void");
                } else {
                    this._errorSafeExtensions.serializeSafely(((JvmOperation) it_1).getReturnType(), "Object", tracedAppendable);
                }
                tracedAppendable.append(" ");
                this._treeAppendableUtil.traceSignificant(tracedAppendable, it_1).append(((JvmOperation) it_1).getSimpleName());
                tracedAppendable.append("(");
                this.generateParameters(((JvmExecutable) it_1), tracedAppendable, null);
                tracedAppendable.append(")");
                this.generateThrowsClause(((JvmExecutable) it_1), tracedAppendable, null);
                tracedAppendable.append(";");
            } else {
                if ((it_1 instanceof JvmField)) {
                    final ITreeAppendable tracedAppendable_1 = childAppendable.trace(it_1);
                    tracedAppendable_1.newLine();
                    this.generateJavaDoc(it_1, tracedAppendable_1, config);
                    this.generateAnnotations(((JvmField) it_1).getAnnotations(), tracedAppendable_1, true, config);
                    boolean _isStatic = ((JvmField) it_1).isStatic();
                    if (_isStatic) {
                        tracedAppendable_1.append("static ");
                    }
                    if ((((JvmField) it_1).isFinal() && ((JvmField) it_1).isStatic())) {
                        tracedAppendable_1.append("final ");
                    }
                    boolean _isTransient = ((JvmField) it_1).isTransient();
                    if (_isTransient) {
                        tracedAppendable_1.append("transient ");
                    }
                    boolean _isVolatile = ((JvmField) it_1).isVolatile();
                    if (_isVolatile) {
                        tracedAppendable_1.append("volatile ");
                    }
                    this._errorSafeExtensions.serializeSafely(((JvmField) it_1).getType(), "Object", tracedAppendable_1);
                    tracedAppendable_1.append(" ");
                    this._treeAppendableUtil.traceSignificant(tracedAppendable_1, it_1).append(((JvmField) it_1).getSimpleName());
                    if ((((JvmField) it_1).isFinal() && ((JvmField) it_1).isStatic())) {
                        Object _constantValue = ((JvmField) it_1).getConstantValue();
                        boolean _tripleNotEquals = (_constantValue != null);
                        if (_tripleNotEquals) {
                            tracedAppendable_1.append(" = ");
                            this.generateJavaConstant(((JvmField) it_1).getConstantValue(), tracedAppendable_1);
                        } else {
                            this.generateInitialization(((JvmField) it_1), tracedAppendable_1, config);
                        }
                    }
                    tracedAppendable_1.append(";");
                } else {
                    this.generateMember(it_1, memberAppendable, config);
                }
            }
            memberAppendable.closeScope();
        };
        this._loopExtensions.<JvmMember>forEach(childAppendable, this.getAddedDeclarations(it, anonymousClass), _function_2, _function_3);
        childAppendable.decreaseIndentation().newLine().append("}");
        appendable.newLine();
    };
    IterableExtensions.<JvmGenericType>filter(feature.getLocalClasses(), _function).forEach(_function_1);
}
Also used : JvmTypeParameterDeclarator(org.eclipse.xtext.common.types.JvmTypeParameterDeclarator) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) ITreeAppendable(org.eclipse.xtext.xbase.compiler.output.ITreeAppendable) LoopParams(org.eclipse.xtext.xbase.compiler.LoopParams) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) JvmMember(org.eclipse.xtext.common.types.JvmMember) EObject(org.eclipse.emf.ecore.EObject) JvmField(org.eclipse.xtext.common.types.JvmField) Pair(org.eclipse.xtext.xbase.lib.Pair)

Aggregations

EObject (org.eclipse.emf.ecore.EObject)3 JvmTypeParameterDeclarator (org.eclipse.xtext.common.types.JvmTypeParameterDeclarator)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 EList (org.eclipse.emf.common.util.EList)2 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)2 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)2 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)2 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)2 EClass (org.eclipse.emf.ecore.EClass)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)1 JvmExecutable (org.eclipse.xtext.common.types.JvmExecutable)1 JvmField (org.eclipse.xtext.common.types.JvmField)1 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)1 JvmMember (org.eclipse.xtext.common.types.JvmMember)1 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)1 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)1 IJvmTypeProvider (org.eclipse.xtext.common.types.access.IJvmTypeProvider)1 AbstractTypeScope (org.eclipse.xtext.common.types.xtext.AbstractTypeScope)1