Search in sources :

Example 16 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource 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 17 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class ComposedMemberScope method getOrCreateComposedMemberCache.

/**
 * Returns the composed member cache for the given ComposedTypeRef, creating it if it does not exist yet. Returns
 * <code>null</code> if a cache could not be created, because the given type reference is not contained in an
 * N4JSResource or this resource does not have a TModule.
 */
private ComposedMemberCache getOrCreateComposedMemberCache() {
    if (request.provideContainedMembers) {
        final // 
        MemberAccess contextCasted = // cast is valid, see MemberScopeRequest#provideContainedMembers
        (MemberAccess) request.context;
        final ComposedMemberCache cache = contextCasted.getComposedMemberCache();
        if (cache != null && TypeCompareUtils.isEqual(cache.getComposedTypeRef(), this.composedTypeRef)) {
            return cache;
        }
        // does not exist yet -> create new composed member cache in TModule:
        final Resource res = contextCasted.eResource();
        final TModule module = res instanceof N4JSResource ? ((N4JSResource) res).getModule() : null;
        if (module != null) {
            // Search in the module for composed member cache containing equivalent composed type ref
            for (ComposedMemberCache existingCache : module.getComposedMemberCaches()) {
                if (TypeCompareUtils.isEqual(existingCache.getComposedTypeRef(), composedTypeRef)) {
                    return existingCache;
                }
            }
            final ComposedMemberCache cacheNew = TypesFactory.eINSTANCE.createComposedMemberCache();
            EcoreUtilN4.doWithDeliver(false, () -> {
                // Order important due to notification!
                cacheNew.setComposedTypeRef(TypeUtils.copyIfContained(composedTypeRef));
                module.getComposedMemberCaches().add(cacheNew);
                contextCasted.setComposedMemberCache(cacheNew);
            }, module, contextCasted);
            return cacheNew;
        }
    }
    return null;
}
Also used : MemberAccess(org.eclipse.n4js.n4JS.MemberAccess) N4JSResource(org.eclipse.n4js.resource.N4JSResource) Resource(org.eclipse.emf.ecore.resource.Resource) N4JSResource(org.eclipse.n4js.resource.N4JSResource) ComposedMemberCache(org.eclipse.n4js.ts.types.ComposedMemberCache) TModule(org.eclipse.n4js.ts.types.TModule)

Example 18 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class StaticPolyfillHelper method hasStaticPolyfill.

/**
 * For a given N4JSResource annotated with {@code @@StaticPolyfillAware} lookup the filling Module. returns
 * {@code true} if the filling Module exists in the project.
 */
public boolean hasStaticPolyfill(Resource resource) {
    // ensure right resource
    if (resource instanceof N4JSResource) {
        final N4JSResource res = (N4JSResource) resource;
        if (isContainedInStaticPolyfillAware(res.getScript())) {
            // TODO GHOLD-196 resolve inconsistency in logic between this method and #findStaticPolyfiller(Resource)
            // (i.e. if possible, delete strategy #1 and only use strategy #2; but make sure this isn't a
            // performance issue, esp. with respect to the call "srcConti.findArtifact(fqn, fileExtension)" in
            // #findStaticPolyfiller(Resource))
            boolean strategyIndex = true;
            if (strategyIndex) {
                // 1. query index
                final QualifiedName qnFilled = qualifiedNameConverter.toQualifiedName(res.getModule().getQualifiedName());
                final IResourceDescriptions index = indexAccess.getResourceDescriptions(res.getResourceSet());
                final java.util.Optional<QualifiedName> optQnFilling = N4TSQualifiedNameProvider.toStaticPolyfillFQN(qnFilled);
                if (optQnFilling.isPresent()) {
                    final QualifiedName qnFilling = optQnFilling.get();
                    final Iterable<IEObjectDescription> modules = index.getExportedObjectsByType(TypesPackage.Literals.TMODULE);
                    for (IEObjectDescription module : modules) {
                        if (module.getQualifiedName() == qnFilling) {
                            return true;
                        }
                    }
                }
            } else {
                // 2. query all source-containers for file with same QN
                final URI fillingURI = findStaticPolyfiller(res);
                if (null != fillingURI)
                    return true;
            }
        }
    }
    return false;
}
Also used : QualifiedName(org.eclipse.xtext.naming.QualifiedName) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) N4JSResource(org.eclipse.n4js.resource.N4JSResource) URI(org.eclipse.emf.common.util.URI) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 19 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class StaticPolyfillHelper method findStaticPolyfiller.

/**
 * Find the corresponding static-polyfill to this {@code @@PolyfillAware} resource in the same project. returns null
 * if not found or this resource has no {@code @@PolyfillAware} annotation.
 */
public URI findStaticPolyfiller(Resource resource) {
    // ensure right resource
    if (resource instanceof N4JSResource) {
        final N4JSResource res = (N4JSResource) resource;
        if (!isContainedInStaticPolyfillAware(res.getScript()))
            return null;
        final QualifiedName qnFilled = qualifiedNameConverter.toQualifiedName(res.getModule().getQualifiedName());
        final IN4JSProject project = projectResolver.resolveProject(res.getURI());
        final QualifiedName fqn = qnFilled;
        // see Req.155#4: "Both
        final Optional<String> fileExtension = Optional.of(res.getURI().fileExtension());
        // extensions are
        // equal."
        final IN4JSSourceContainer filledSrcContainer = n4jsCore.findN4JSSourceContainer(res.getURI()).get();
        for (IN4JSSourceContainer srcConti : project.getSourceContainers()) {
            if (!Objects.equals(filledSrcContainer, srcConti)) {
                final URI uri = srcConti.findArtifact(fqn, fileExtension);
                if (uri != null) {
                    return uri;
                }
            }
        }
    }
    return null;
}
Also used : IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) QualifiedName(org.eclipse.xtext.naming.QualifiedName) N4JSResource(org.eclipse.n4js.resource.N4JSResource) URI(org.eclipse.emf.common.util.URI) IN4JSSourceContainer(org.eclipse.n4js.projectModel.IN4JSSourceContainer)

Example 20 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class N4JSResourceValidator method validate.

@Override
public List<Issue> validate(Resource resource, CheckMode mode, CancelIndicator cancelIndicator) {
    if (resource instanceof N4JSResource) {
        final N4JSResource resourceCasted = (N4JSResource) resource;
        try {
            resourceCasted.performPostProcessing(cancelIndicator);
        } catch (Throwable th) {
        // ignore this exception/error (we will create an issue for it below)
        }
        if (resourceCasted.isFullyProcessed() && resourceCasted.getPostProcessingThrowable() != null) {
            // When getting here, we have an attempt to validate a resource that was post-processed but the
            // post-processing failed (i.e. postProcessingThrowable!=null). Validating such a resource will usually
            // cause a multitude of follow-up exceptions in many @Check methods (could easily be hundreds). Since,
            // EMF/Xtext has the behavior of not aborting the overall validation due to an exception in a single
            // @Check method, this will lead to many exceptions, which are all just follow-up issues of the problem
            // that caused the post-processing to fail.
            // 1) Since this is annoying and misleading, we just ignore all ordinary validation.
            // 2) To not overlook the problem, we create a single validation issue (error) pointing out the problem.
            // (for a test, see class AvoidFollowUpExceptionsInValidationTest)
            final Throwable th = resourceCasted.getPostProcessingThrowable();
            if (operationCanceledManager.isOperationCanceledException(th)) {
                // do not show errors in case of cancellation
                return Collections.emptyList();
            }
            final Issue issue = createPostProcessingFailedError(resourceCasted, th);
            return Collections.singletonList(issue);
        }
    }
    return super.validate(resource, mode, cancelIndicator);
}
Also used : Issue(org.eclipse.xtext.validation.Issue) N4JSResource(org.eclipse.n4js.resource.N4JSResource)

Aggregations

N4JSResource (org.eclipse.n4js.resource.N4JSResource)21 Resource (org.eclipse.emf.ecore.resource.Resource)10 URI (org.eclipse.emf.common.util.URI)8 EObject (org.eclipse.emf.ecore.EObject)6 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)5 Script (org.eclipse.n4js.n4JS.Script)4 IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)3 TModule (org.eclipse.n4js.ts.types.TModule)3 Optional (com.google.common.base.Optional)2 Iterables (com.google.common.collect.Iterables)2 File (java.io.File)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2 Path (java.nio.file.Path)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 QualifiedName (org.eclipse.xtext.naming.QualifiedName)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2