Ejemplo n.º 1
0
  @Nullable
  /*package*/ SNode getTypeOf(final SNode node) {
    ModelAccess.assertLegalRead();
    if (node == null) return null;

    if (myTypeChecker.isGenerationMode()) {
      TypeCheckingContext context =
          myTypeChecker.hasPerformanceTracer()
              ? new TargetTypecheckingContext_Tracer(node, myTypeChecker)
              : new TargetTypecheckingContext(node, myTypeChecker);
      try {
        return context.getTypeOf_generationMode(node);
      } finally {
        context.dispose();
      }
    }
    // now we are not in generation mode

    final ITypeContextOwner contextOwner = myTypecheckingContextOwner.get();
    TypeCheckingContext context = acquireTypecheckingContext(node, contextOwner);
    if (context == null) {
      return TypesUtil.createRuntimeErrorType();
    }

    try {
      return context.getTypeOf(node, myTypeChecker);
    } finally {
      releaseTypecheckingContext(node, contextOwner);
    }
  }
Ejemplo n.º 2
0
 @Override
 public void release() {
   if (!myContexts.isEmpty()) {
     final TypeCheckingContext ctx = myContexts.removeLast();
     ctx.dispose();
   }
 }
Ejemplo n.º 3
0
 @Override
 public void dispose() {
   for (TypeCheckingContext context : myContexts) {
     context.dispose();
   }
   myContexts.clear();
 }
Ejemplo n.º 4
0
 @Override
 public void release() {
   assert myCount > 0;
   if ((myCount -= 1) <= 0) {
     myContext.dispose();
     myContext = null;
   }
 }
Ejemplo n.º 5
0
 @Override
 public void dispose() {
   if (myContext != null) {
     myContext.dispose();
   }
   myContext = null;
   myCount = 0;
 }
Ejemplo n.º 6
0
    @Override
    public TypeCheckingContext acquire(SNode node) {
      if (myContexts.size() >= 10) {
        LOG.warn("too many non-reusable typechecking contexts");
        return null;
      }
      for (TypeCheckingContext ctx : myContexts) {
        if (ctx.getNode() == node) {
          LOG.warn("double typechecking context acquiring");
          return null;
        }
      }

      final TypeCheckingContext ctx =
          myOwner.createTypecheckingContext(node, TypeContextManager.this);
      addModelListener(node);
      myContexts.add(ctx);

      return ctx;
    }
Ejemplo n.º 7
0
 @Override
 public void clear() {
   for (TypeCheckingContext context : myContexts) {
     context.clear();
   }
 }
Ejemplo n.º 8
0
 @Override
 public void clear() {
   if (myContext != null) {
     myContext.clear();
   }
 }
Ejemplo n.º 9
0
  private TypeCheckingContext getOrCreateContext(
      SNode node, ITypeContextOwner owner, boolean createIfAbsent) {
    ModelAccess.assertLegalRead();
    if (node == null) return null;
    final SNode rootNode = node.getContainingRoot();
    synchronized (myLock) {
      SNodeReference rootNodePointer = new jetbrains.mps.smodel.SNodePointer(rootNode);

      List<TypecheckingContextHolder> contextWithOwners =
          myTypeCheckingContexts.get(rootNodePointer);
      if (contextWithOwners == null && !createIfAbsent) return null;
      if (contextWithOwners == null) {
        contextWithOwners = new ArrayList<TypecheckingContextHolder>(4);
        myTypeCheckingContexts.put(rootNodePointer, contextWithOwners);
      }

      for (ListIterator<TypecheckingContextHolder> it = contextWithOwners.listIterator();
          it.hasNext(); ) {
        TypecheckingContextHolder contextHolder = it.next();
        if (contextHolder.getOwner() == owner) {

          if (!owner.reuseTypecheckingContext()) {
            assert createIfAbsent;
            return contextHolder.acquire(node);
          } else {
            // reuse the typechecking context
            if (!createIfAbsent) {
              return contextHolder.get(node);
            }

            final TypeCheckingContext ctx = contextHolder.acquire(node);

            // Dirty hack
            if (jetbrains.mps.util.SNodeOperations.isDisposed(ctx.getNode())) {
              removeContextForNode(rootNodePointer);
              LOG.error(
                  "Type Checking Context had a disposed node inside. Node: "
                      + node
                      + " model: "
                      + node.getModel());
              return getOrCreateContext(node, owner, createIfAbsent);
            }

            return ctx;
          }
        }
      }

      // not found, create new
      if (!owner.reuseTypecheckingContext()) {
        assert createIfAbsent;

        final NonReusableTypecheckingContextHolder contextHolder =
            new NonReusableTypecheckingContextHolder(owner);
        contextWithOwners.add(contextHolder);

        return contextHolder.acquire(node);
      } else if (!createIfAbsent) {
        return null;
      } else {
        if (contextWithOwners.size() > 100) {
          if (!myReported) {
            myReported = true;
            LOG.warn(
                "Type checking context for node "
                    + node.getPresentation()
                    + " has too much owners");
          }
        }

        final CountingTypecheckingContextHolder contextHolder =
            new CountingTypecheckingContextHolder(owner);
        contextWithOwners.add(contextHolder);
        return contextHolder.acquire(node);
      }
    }
  }