public void manageSyntheticAccessIfNecessary(BlockScope currentScope, FlowInfo flowInfo) {
    if ((flowInfo.tagBits & FlowInfo.UNREACHABLE_OR_DEAD) == 0) {
      // need assertion flag: $assertionsDisabled on outer most source clas
      // (in case of static member of interface, will use the outermost static member - bug 22334)
      SourceTypeBinding outerMostClass = currentScope.enclosingSourceType();
      while (outerMostClass.isLocalType()) {
        ReferenceBinding enclosing = outerMostClass.enclosingType();
        if (enclosing == null || enclosing.isInterface()) break;
        outerMostClass = (SourceTypeBinding) enclosing;
      }
      this.assertionSyntheticFieldBinding = outerMostClass.addSyntheticFieldForAssert(currentScope);

      // find <clinit> and enable assertion support
      TypeDeclaration typeDeclaration = outerMostClass.scope.referenceType();
      AbstractMethodDeclaration[] methods = typeDeclaration.methods;
      for (int i = 0, max = methods.length; i < max; i++) {
        AbstractMethodDeclaration method = methods[i];
        if (method.isClinit()) {
          ((Clinit) method)
              .setAssertionSupport(
                  this.assertionSyntheticFieldBinding,
                  currentScope.compilerOptions().sourceLevel < ClassFileConstants.JDK1_5);
          break;
        }
      }
    }
  }
  public TypeBinding resolveType(BlockScope scope) {
    this.constant = Constant.NotAConstant;
    TypeBinding expressionType = this.expression.resolveType(scope);
    TypeBinding checkedType = this.type.resolveType(scope, true /* check bounds*/);
    if (expressionType == null || checkedType == null) return null;

    if (!checkedType.isReifiable()) {
      scope.problemReporter().illegalInstanceOfGenericType(checkedType, this);
    } else if ((expressionType != TypeBinding.NULL
            && expressionType.isBaseType()) // disallow autoboxing
        || !checkCastTypesCompatibility(scope, checkedType, expressionType, null)) {
      scope.problemReporter().notCompatibleTypesError(this, expressionType, checkedType);
    }
    return this.resolvedType = TypeBinding.BOOLEAN;
  }
示例#3
0
 void checkAssignment(BlockScope scope, TypeBinding lhsType, TypeBinding rhsType) {
   FieldBinding leftField = getLastField(this.lhs);
   if (leftField != null
       && rhsType != TypeBinding.NULL
       && (lhsType.kind() == Binding.WILDCARD_TYPE)
       && ((WildcardBinding) lhsType).boundKind != Wildcard.SUPER) {
     scope.problemReporter().wildcardAssignment(lhsType, rhsType, this.expression);
   } else if (leftField != null
       && !leftField.isStatic()
       && leftField.declaringClass != null /*length pseudo field*/
       && leftField.declaringClass.isRawType()) {
     scope.problemReporter().unsafeRawFieldAssignment(leftField, rhsType, this.lhs);
   } else if (rhsType.needsUncheckedConversion(lhsType)) {
     scope.problemReporter().unsafeTypeConversion(this.expression, rhsType, lhsType);
   }
 }
 public void resolve(BlockScope scope) {
   this.assertExpression.resolveTypeExpecting(scope, TypeBinding.BOOLEAN);
   if (this.exceptionArgument != null) {
     TypeBinding exceptionArgumentType = this.exceptionArgument.resolveType(scope);
     if (exceptionArgumentType != null) {
       int id = exceptionArgumentType.id;
       switch (id) {
         case T_void:
           scope.problemReporter().illegalVoidExpression(this.exceptionArgument);
           // $FALL-THROUGH$
         default:
           id = T_JavaLangObject;
           // $FALL-THROUGH$
         case T_boolean:
         case T_byte:
         case T_char:
         case T_short:
         case T_double:
         case T_float:
         case T_int:
         case T_long:
         case T_JavaLangString:
           this.exceptionArgument.implicitConversion = (id << 4) + id;
       }
     }
   }
 }
  public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {

    // {ObjectTeams: role class literal?
    if (this.roleClassLiteralAccess != null)
      return this.roleClassLiteralAccess.analyseCode(currentScope, flowContext, flowInfo);
    // SH}
    // if reachable, request the addition of a synthetic field for caching the class descriptor
    SourceTypeBinding sourceType = currentScope.outerMostClassScope().enclosingSourceType();
    // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=22334
    if (!sourceType.isInterface()
        && !this.targetType.isBaseType()
        && currentScope.compilerOptions().targetJDK < ClassFileConstants.JDK1_5) {
      this.syntheticField =
          sourceType.addSyntheticFieldForClassLiteral(this.targetType, currentScope);
    }
    return flowInfo;
  }
  /* Inner emulation consists in either recording a dependency
   * link only, or performing one level of propagation.
   *
   * Dependency mechanism is used whenever dealing with source target
   * types, since by the time we reach them, we might not yet know their
   * exact need.
   */
  public void manageEnclosingInstanceAccessIfNecessary(BlockScope currentScope, FlowInfo flowInfo) {
    if ((flowInfo.tagBits & FlowInfo.UNREACHABLE_OR_DEAD) != 0) return;
    ReferenceBinding allocatedTypeErasure =
        (ReferenceBinding) this.binding.declaringClass.erasure();

    // perform some emulation work in case there is some and we are inside a local type only
    if (allocatedTypeErasure.isNestedType() && currentScope.enclosingSourceType().isLocalType()) {

      if (allocatedTypeErasure.isLocalType()) {
        ((LocalTypeBinding) allocatedTypeErasure).addInnerEmulationDependent(currentScope, false);
        // request cascade of accesses
      } else {
        // locally propagate, since we already now the desired shape for sure
        currentScope.propagateInnerEmulation(allocatedTypeErasure, false);
        // request cascade of accesses
      }
    }
  }
 public void checkCapturedLocalInitializationIfNecessary(
     ReferenceBinding checkedType, BlockScope currentScope, FlowInfo flowInfo) {
   if (((checkedType.tagBits & (TagBits.AnonymousTypeMask | TagBits.LocalTypeMask))
           == TagBits.LocalTypeMask)
       && !currentScope.isDefinedInType(checkedType)) { // only check external allocations
     NestedTypeBinding nestedType = (NestedTypeBinding) checkedType;
     SyntheticArgumentBinding[] syntheticArguments = nestedType.syntheticOuterLocalVariables();
     if (syntheticArguments != null)
       for (int i = 0, count = syntheticArguments.length; i < count; i++) {
         SyntheticArgumentBinding syntheticArgument = syntheticArguments[i];
         LocalVariableBinding targetLocal;
         if ((targetLocal = syntheticArgument.actualOuterLocalVariable) == null) continue;
         if (targetLocal.declaration != null && !flowInfo.isDefinitelyAssigned(targetLocal)) {
           currentScope.problemReporter().uninitializedLocalVariable(targetLocal, this);
         }
       }
   }
 }
 /**
  * @see
  *     org.eclipse.jdt.internal.compiler.ast.BinaryExpression#resolveType(org.eclipse.jdt.internal.compiler.lookup.BlockScope)
  */
 public TypeBinding resolveType(BlockScope scope) {
   TypeBinding result = super.resolveType(scope);
   // check whether comparing identical expressions
   Binding leftDirect = Expression.getDirectBinding(this.left);
   if (leftDirect != null && leftDirect == Expression.getDirectBinding(this.right)) {
     if (!(this.right instanceof Assignment))
       scope.problemReporter().comparingIdenticalExpressions(this);
   }
   return result;
 }
 // Report an error if necessary (if even more unreachable than previously reported
 // complaintLevel = 0 if was reachable up until now, 1 if fake reachable (deadcode), 2 if fatal
 // unreachable (error)
 public int complainIfUnreachable(
     FlowInfo flowInfo, BlockScope scope, int previousComplaintLevel, boolean endOfBlock) {
   if ((flowInfo.reachMode() & FlowInfo.UNREACHABLE) != 0) {
     if ((flowInfo.reachMode() & FlowInfo.UNREACHABLE_OR_DEAD) != 0)
       this.bits &= ~ASTNode.IsReachable;
     if (flowInfo == FlowInfo.DEAD_END) {
       if (previousComplaintLevel < COMPLAINED_UNREACHABLE) {
         scope.problemReporter().unreachableCode(this);
         if (endOfBlock) scope.checkUnclosedCloseables(flowInfo, null, null, null);
       }
       return COMPLAINED_UNREACHABLE;
     } else {
       if (previousComplaintLevel < COMPLAINED_FAKE_REACHABLE) {
         scope.problemReporter().fakeReachable(this);
         if (endOfBlock) scope.checkUnclosedCloseables(flowInfo, null, null, null);
       }
       return COMPLAINED_FAKE_REACHABLE;
     }
   }
   return previousComplaintLevel;
 }
示例#10
0
  // Report an error if necessary
  public boolean complainIfUnreachable(
      FlowInfo flowInfo, BlockScope scope, boolean didAlreadyComplain) {

    if ((flowInfo.reachMode() & FlowInfo.UNREACHABLE) != 0) {
      this.bits &= ~ASTNode.IsReachableMASK;
      boolean reported = flowInfo == FlowInfo.DEAD_END;
      if (!didAlreadyComplain && reported) {
        scope.problemReporter().unreachableCode(this);
      }
      return reported; // keep going for fake reachable
    }
    return false;
  }
 public void checkTypeArgumentRedundancy(
     ParameterizedTypeBinding allocationType,
     ReferenceBinding enclosingType,
     TypeBinding[] argumentTypes,
     final BlockScope scope) {
   ProblemReporter reporter = scope.problemReporter();
   if ((reporter.computeSeverity(IProblem.RedundantSpecificationOfTypeArguments)
           == ProblemSeverities.Ignore)
       || scope.compilerOptions().sourceLevel < ClassFileConstants.JDK1_7) return;
   if (allocationType.arguments == null) return; // raw binding
   if (this.genericTypeArguments != null)
     return; // diamond can't occur with explicit type args for constructor
   if (argumentTypes == Binding.NO_PARAMETERS
       && this.typeExpected instanceof ParameterizedTypeBinding) {
     ParameterizedTypeBinding expected = (ParameterizedTypeBinding) this.typeExpected;
     if (expected.arguments != null
         && allocationType.arguments.length == expected.arguments.length) {
       // check the case when no ctor takes no params and inference uses the expected type directly
       // eg. X<String> x = new X<String>()
       int i;
       for (i = 0; i < allocationType.arguments.length; i++) {
         if (allocationType.arguments[i] != expected.arguments[i]) break;
       }
       if (i == allocationType.arguments.length) {
         reporter.redundantSpecificationOfTypeArguments(this.type, allocationType.arguments);
         return;
       }
     }
   }
   TypeBinding[] inferredTypes =
       inferElidedTypes(allocationType.genericType(), enclosingType, argumentTypes, scope);
   if (inferredTypes == null) {
     return;
   }
   for (int i = 0; i < inferredTypes.length; i++) {
     if (inferredTypes[i] != allocationType.arguments[i]) return;
   }
   reporter.redundantSpecificationOfTypeArguments(this.type, allocationType.arguments);
 }
示例#12
0
  public TypeBinding resolveType(BlockScope scope) {
    // due to syntax lhs may be only a NameReference, a FieldReference or an ArrayReference
    this.constant = Constant.NotAConstant;
    if (!(this.lhs instanceof Reference) || this.lhs.isThis()) {
      scope.problemReporter().expressionShouldBeAVariable(this.lhs);
      return null;
    }
    TypeBinding lhsType = lhs.resolveType(scope);
    this.expression.setExpectedType(lhsType); // needed in case of generic method invocation
    if (lhsType != null) {
      this.resolvedType = lhsType.capture(scope, this.sourceEnd);
    }
    TypeBinding rhsType = this.expression.resolveType(scope);
    if (lhsType == null || rhsType == null) {
      return null;
    }
    // check for assignment with no effect
    Binding left = getDirectBinding(this.lhs);
    if (left != null && left == getDirectBinding(this.expression)) {
      scope.problemReporter().assignmentHasNoEffect(this, left.shortReadableName());
    }

    // Compile-time conversion of base-types : implicit narrowing integer into byte/short/character
    // may require to widen the rhs expression at runtime
    if (lhsType != rhsType) { // must call before computeConversion() and typeMismatchError()
      scope.compilationUnitScope().recordTypeConversion(lhsType, rhsType);
    }
    if ((this.expression.isConstantValueOfTypeAssignableToType(rhsType, lhsType)
            || (lhsType.isBaseType() && BaseTypeBinding.isWidening(lhsType.id, rhsType.id)))
        || rhsType.isCompatibleWith(lhsType)) {
      this.expression.computeConversion(scope, lhsType, rhsType);
      checkAssignment(scope, lhsType, rhsType);
      if (this.expression instanceof CastExpression
          && (this.expression.bits & ASTNode.UnnecessaryCast) == 0) {
        CastExpression.checkNeedForAssignedCast(scope, lhsType, (CastExpression) this.expression);
      }
      return this.resolvedType;
    } else if (scope.isBoxingCompatibleWith(rhsType, lhsType)
        || (rhsType.isBaseType() // narrowing then boxing ?
            && scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5 // autoboxing
            && !lhsType.isBaseType()
            && this.expression.isConstantValueOfTypeAssignableToType(
                rhsType, scope.environment().computeBoxingType(lhsType)))) {
      this.expression.computeConversion(scope, lhsType, rhsType);
      if (this.expression instanceof CastExpression
          && (this.expression.bits & ASTNode.UnnecessaryCast) == 0) {
        CastExpression.checkNeedForAssignedCast(scope, lhsType, (CastExpression) this.expression);
      }
      return this.resolvedType;
    }
    scope.problemReporter().typeMismatchError(rhsType, lhsType, this.expression, this.lhs);
    return lhsType;
  }
示例#13
0
 /** Check null-ness of 'var' against a possible null annotation */
 protected int checkAssignmentAgainstNullAnnotation(
     BlockScope currentScope,
     FlowContext flowContext,
     VariableBinding var,
     int nullStatus,
     Expression expression,
     TypeBinding providedType) {
   int severity = 0;
   if ((var.tagBits & TagBits.AnnotationNonNull) != 0 && nullStatus != FlowInfo.NON_NULL) {
     flowContext.recordNullityMismatch(
         currentScope, expression, providedType, var.type, nullStatus);
     return FlowInfo.NON_NULL;
   } else if ((severity = findNullTypeAnnotationMismatch(var.type, providedType)) > 0) {
     currentScope
         .problemReporter()
         .nullityMismatchingTypeAnnotation(
             expression, providedType, var.type, severity == 1, currentScope.environment());
   } else if ((var.tagBits & TagBits.AnnotationNullable) != 0
       && nullStatus == FlowInfo.UNKNOWN) { // provided a legacy type?
     return FlowInfo.POTENTIALLY_NULL; // -> use more specific info from the annotation
   }
   return nullStatus;
 }
  public void manageSyntheticAccessIfNecessary(BlockScope currentScope, FlowInfo flowInfo) {
    if ((flowInfo.tagBits & FlowInfo.UNREACHABLE_OR_DEAD) != 0) return;
    // if constructor from parameterized type got found, use the original constructor at codegen
    // time
    MethodBinding codegenBinding = this.binding.original();

    ReferenceBinding declaringClass;
    if (codegenBinding.isPrivate()
        && currentScope.enclosingSourceType() != (declaringClass = codegenBinding.declaringClass)) {

      // from 1.4 on, local type constructor can lose their private flag to ease emulation
      if ((declaringClass.tagBits & TagBits.IsLocalType) != 0
          && currentScope.compilerOptions().complianceLevel >= ClassFileConstants.JDK1_4) {
        // constructor will not be dumped as private, no emulation required thus
        codegenBinding.tagBits |= TagBits.ClearPrivateModifier;
      } else {
        this.syntheticAccessor =
            ((SourceTypeBinding) declaringClass)
                .addSyntheticMethod(codegenBinding, isSuperAccess());
        currentScope.problemReporter().needToEmulateMethodAccess(codegenBinding, this);
      }
    }
  }
示例#15
0
  /**
   * @see
   *     xap.lui.compile.ca.jdt.internal.compiler.ast.Expression#resolveTypeExpecting(xap.lui.compile.ca.jdt.internal.compiler.lookup.BlockScope,
   *     xap.lui.compile.ca.jdt.internal.compiler.lookup.TypeBinding)
   */
  public TypeBinding resolveTypeExpecting(BlockScope scope, TypeBinding expectedType) {

    TypeBinding type = super.resolveTypeExpecting(scope, expectedType);
    if (type == null) return null;
    TypeBinding lhsType = this.resolvedType;
    TypeBinding rhsType = this.expression.resolvedType;
    // signal possible accidental boolean assignment (instead of using '==' operator)
    if (expectedType == TypeBinding.BOOLEAN
        && lhsType == TypeBinding.BOOLEAN
        && (this.lhs.bits & IsStrictlyAssigned) != 0) {
      scope.problemReporter().possibleAccidentalBooleanAssignment(this);
    }
    checkAssignment(scope, lhsType, rhsType);
    return type;
  }
示例#16
0
  /* (non-Javadoc)
   * @see org.eclipse.jdt.internal.compiler.ast.SubRoutineStatement#generateSubRoutineInvocation(org.eclipse.jdt.internal.compiler.lookup.BlockScope, org.eclipse.jdt.internal.compiler.codegen.CodeStream)
   */
  public void generateSubRoutineInvocation(BlockScope currentScope, CodeStream codeStream) {

    if (this.isSubRoutineEscaping) {
      codeStream.goto_(this.subRoutineStartLabel);
    } else {
      if (currentScope.compilerOptions().inlineJsrBytecode) {
        // cannot use jsr bytecode, then simply inline the subroutine
        this.exitAnyExceptionHandler();
        this.finallyBlock.generateCode(currentScope, codeStream);
        this.enterAnyExceptionHandler(codeStream);
      } else {
        // classic subroutine invocation, distinguish case of non-returning subroutine
        codeStream.jsr(this.subRoutineStartLabel);
      }
    }
  }
  public TypeBinding resolveType(BlockScope scope) {
    this.constant = Constant.NotAConstant;
    if (this.arguments != null) {
      int argsLength = this.arguments.length;
      for (int a = argsLength; --a >= 0; ) this.arguments[a].resolveType(scope);
    }

    if (this.receiver.isImplicitThis()) throw new CompletionNodeFound(this, null, scope);

    this.actualReceiverType = this.receiver.resolveType(scope);
    if (this.actualReceiverType == null || this.actualReceiverType.isBaseType())
      throw new CompletionNodeFound();

    if (this.actualReceiverType.isArrayType()) this.actualReceiverType = scope.getJavaLangObject();
    throw new CompletionNodeFound(this, this.actualReceiverType, scope);
  }
 public TypeBinding[] inferElidedTypes(
     ReferenceBinding allocationType,
     ReferenceBinding enclosingType,
     TypeBinding[] argumentTypes,
     final BlockScope scope) {
   /* Given the allocation type and the arguments to the constructor, see if we can synthesize a generic static factory
     method that would, given the argument types and the invocation site, manufacture a parameterized object of type allocationType.
     If we are successful then by design and construction, the parameterization of the return type of the factory method is identical
     to the types elided in the <>.
   */
   MethodBinding factory =
       scope.getStaticFactory(allocationType, enclosingType, argumentTypes, this);
   if (factory instanceof ParameterizedGenericMethodBinding && factory.isValidBinding()) {
     ParameterizedGenericMethodBinding genericFactory =
         (ParameterizedGenericMethodBinding) factory;
     this.inferredReturnType = genericFactory.inferredReturnType;
     return ((ParameterizedTypeBinding) factory.returnType).arguments;
   }
   return null;
 }
示例#19
0
  private static int checkInvocationArgument(
      BlockScope scope,
      Expression argument,
      TypeBinding parameterType,
      TypeBinding argumentType,
      TypeBinding originalParameterType) {
    argument.computeConversion(scope, parameterType, argumentType);

    if (argumentType != TypeBinding.NULL
        && parameterType.kind() == Binding.WILDCARD_TYPE) { // intersection types are tolerated
      WildcardBinding wildcard = (WildcardBinding) parameterType;
      if (wildcard.boundKind != Wildcard.SUPER) {
        return INVOCATION_ARGUMENT_WILDCARD;
      }
    }
    TypeBinding checkedParameterType =
        parameterType; // originalParameterType == null ? parameterType : originalParameterType;
    if (argumentType != checkedParameterType
        && argumentType.needsUncheckedConversion(checkedParameterType)) {
      scope.problemReporter().unsafeTypeConversion(argument, argumentType, checkedParameterType);
      return INVOCATION_ARGUMENT_UNCHECKED;
    }
    return INVOCATION_ARGUMENT_OK;
  }
示例#20
0
  /**
   * Try statement code generation with or without jsr bytecode use post 1.5 target level, cannot
   * use jsr bytecode, must instead inline finally block returnAddress is only allocated if jsr is
   * allowed
   */
  public void generateCode(BlockScope currentScope, CodeStream codeStream) {
    if ((bits & IsReachableMASK) == 0) {
      return;
    }
    // in case the labels needs to be reinitialized
    // when the code generation is restarted in wide mode
    if (this.anyExceptionLabelsCount > 0) {
      this.anyExceptionLabels = NO_EXCEPTION_HANDLER;
      this.anyExceptionLabelsCount = 0;
    }
    int pc = codeStream.position;
    final int NO_FINALLY = 0; // no finally block
    final int FINALLY_SUBROUTINE =
        1; // finally is generated as a subroutine (using jsr/ret bytecodes)
    final int FINALLY_DOES_NOT_COMPLETE =
        2; // non returning finally is optimized with only one instance of finally block
    final int FINALLY_MUST_BE_INLINED =
        3; // finally block must be inlined since cannot use jsr/ret bytecodes >1.5
    int finallyMode;
    if (subRoutineStartLabel == null) {
      finallyMode = NO_FINALLY;
    } else {
      if (this.isSubRoutineEscaping) {
        finallyMode = FINALLY_DOES_NOT_COMPLETE;
      } else if (scope.compilerOptions().inlineJsrBytecode) {
        finallyMode = FINALLY_MUST_BE_INLINED;
      } else {
        finallyMode = FINALLY_SUBROUTINE;
      }
    }
    boolean requiresNaturalExit = false;
    // preparing exception labels
    int maxCatches;
    ExceptionLabel[] exceptionLabels =
        new ExceptionLabel[maxCatches = catchArguments == null ? 0 : catchArguments.length];
    for (int i = 0; i < maxCatches; i++) {
      exceptionLabels[i] = new ExceptionLabel(codeStream, catchArguments[i].binding.type);
    }
    if (subRoutineStartLabel != null) {
      subRoutineStartLabel.initialize(codeStream);
      this.enterAnyExceptionHandler(codeStream);
    }
    // generate the try block
    tryBlock.generateCode(scope, codeStream);
    boolean tryBlockHasSomeCode = codeStream.position != pc;
    // flag telling if some bytecodes were issued inside the try block

    // place end positions of user-defined exception labels
    if (tryBlockHasSomeCode) {
      // natural exit may require subroutine invocation (if finally != null)
      Label naturalExitLabel = new Label(codeStream);
      if (!tryBlockExit) {
        int position = codeStream.position;
        switch (finallyMode) {
          case FINALLY_SUBROUTINE:
          case FINALLY_MUST_BE_INLINED:
            requiresNaturalExit = true;
            // fall through
          case NO_FINALLY:
            codeStream.goto_(naturalExitLabel);
            break;
          case FINALLY_DOES_NOT_COMPLETE:
            codeStream.goto_(subRoutineStartLabel);
            break;
        }
        codeStream.updateLastRecordedEndPC(tryBlock.scope, position);
        // goto is tagged as part of the try block
      }
      for (int i = 0; i < maxCatches; i++) {
        exceptionLabels[i].placeEnd();
      }
      /* generate sequence of handler, all starting by storing the TOS (exception
      thrown) into their own catch variables, the one specified in the source
      that must denote the handled exception.
      */
      if (catchArguments != null) {
        for (int i = 0; i < maxCatches; i++) {
          // May loose some local variable initializations : affecting the local variable attributes
          if (preTryInitStateIndex != -1) {
            codeStream.removeNotDefinitelyAssignedVariables(currentScope, preTryInitStateIndex);
          }
          exceptionLabels[i].place();
          codeStream.incrStackSize(1);
          // optimizing the case where the exception variable is not actually used
          LocalVariableBinding catchVar;
          int varPC = codeStream.position;
          if ((catchVar = catchArguments[i].binding).resolvedPosition != -1) {
            codeStream.store(catchVar, false);
            catchVar.recordInitializationStartPC(codeStream.position);
            codeStream.addVisibleLocalVariable(catchVar);
          } else {
            codeStream.pop();
          }
          codeStream.recordPositionsFrom(varPC, catchArguments[i].sourceStart);
          // Keep track of the pcs at diverging point for computing the local attribute
          // since not passing the catchScope, the block generation will exitUserScope(catchScope)
          catchBlocks[i].generateCode(scope, codeStream);
          if (!catchExits[i]) {
            switch (finallyMode) {
              case FINALLY_SUBROUTINE:
              case FINALLY_MUST_BE_INLINED:
                requiresNaturalExit = true;
                // fall through
              case NO_FINALLY:
                codeStream.goto_(naturalExitLabel);
                break;
              case FINALLY_DOES_NOT_COMPLETE:
                codeStream.goto_(subRoutineStartLabel);
                break;
            }
          }
        }
      }
      this.exitAnyExceptionHandler();
      // extra handler for trailing natural exit (will be fixed up later on when natural exit is
      // generated below)
      ExceptionLabel naturalExitExceptionHandler =
          finallyMode == FINALLY_SUBROUTINE && requiresNaturalExit
              ? new ExceptionLabel(codeStream, null)
              : null;

      // addition of a special handler so as to ensure that any uncaught exception (or exception
      // thrown
      // inside catch blocks) will run the finally block
      int finallySequenceStartPC = codeStream.position;
      if (subRoutineStartLabel != null) {
        this.placeAllAnyExceptionHandlers();
        if (naturalExitExceptionHandler != null) naturalExitExceptionHandler.place();

        if (preTryInitStateIndex != -1) {
          // reset initialization state, as for a normal catch block
          codeStream.removeNotDefinitelyAssignedVariables(currentScope, preTryInitStateIndex);
        }

        codeStream.incrStackSize(1);
        switch (finallyMode) {
          case FINALLY_SUBROUTINE:
            codeStream.store(anyExceptionVariable, false);
            codeStream.jsr(subRoutineStartLabel);
            codeStream.recordPositionsFrom(finallySequenceStartPC, finallyBlock.sourceStart);
            int position = codeStream.position;
            codeStream.load(anyExceptionVariable);
            codeStream.athrow();
            codeStream.recordPositionsFrom(position, finallyBlock.sourceEnd);
            subRoutineStartLabel.place();
            codeStream.incrStackSize(1);
            position = codeStream.position;
            codeStream.store(returnAddressVariable, false);
            codeStream.recordPositionsFrom(position, finallyBlock.sourceStart);
            finallyBlock.generateCode(scope, codeStream);
            position = codeStream.position;
            codeStream.ret(returnAddressVariable.resolvedPosition);
            //						codeStream.updateLastRecordedEndPC(position);
            codeStream.recordPositionsFrom(position, finallyBlock.sourceEnd);
            // the ret bytecode is part of the subroutine
            break;
          case FINALLY_MUST_BE_INLINED:
            codeStream.store(anyExceptionVariable, false);
            codeStream.recordPositionsFrom(finallySequenceStartPC, finallyBlock.sourceStart);
            this.finallyBlock.generateCode(currentScope, codeStream);
            position = codeStream.position;
            codeStream.load(anyExceptionVariable);
            codeStream.athrow();
            subRoutineStartLabel.place();
            codeStream.recordPositionsFrom(position, finallyBlock.sourceEnd);
            break;
          case FINALLY_DOES_NOT_COMPLETE:
            codeStream.pop();
            subRoutineStartLabel.place();
            codeStream.recordPositionsFrom(finallySequenceStartPC, finallyBlock.sourceStart);
            finallyBlock.generateCode(scope, codeStream);
            break;
        }
        // will naturally fall into subsequent code after subroutine invocation
        naturalExitLabel.place();
        if (requiresNaturalExit) {
          switch (finallyMode) {
            case FINALLY_SUBROUTINE:
              int position = codeStream.position;
              // fix up natural exit handler
              naturalExitExceptionHandler.placeStart();
              codeStream.jsr(subRoutineStartLabel);
              naturalExitExceptionHandler.placeEnd();
              codeStream.recordPositionsFrom(position, finallyBlock.sourceEnd);
              break;
            case FINALLY_MUST_BE_INLINED:
              // May loose some local variable initializations : affecting the local variable
              // attributes
              // needed since any exception handler got inlined subroutine
              if (preTryInitStateIndex != -1) {
                codeStream.removeNotDefinitelyAssignedVariables(currentScope, preTryInitStateIndex);
              }
              // entire sequence for finally is associated to finally block
              finallyBlock.generateCode(scope, codeStream);
              break;
            case FINALLY_DOES_NOT_COMPLETE:
              break;
          }
        }
      } else {
        // no subroutine, simply position end label (natural exit == end)
        naturalExitLabel.place();
      }
    } else {
      // try block had no effect, only generate the body of the finally block if any
      if (subRoutineStartLabel != null) {
        finallyBlock.generateCode(scope, codeStream);
      }
    }
    // May loose some local variable initializations : affecting the local variable attributes
    if (mergedInitStateIndex != -1) {
      codeStream.removeNotDefinitelyAssignedVariables(currentScope, mergedInitStateIndex);
      codeStream.addDefinitelyAssignedVariables(currentScope, mergedInitStateIndex);
    }
    codeStream.recordPositionsFrom(pc, this.sourceStart);
  }
示例#21
0
 /** Analysing arguments of MessageSend, ExplicitConstructorCall, AllocationExpression. */
 protected void analyseArguments(
     BlockScope currentScope,
     FlowContext flowContext,
     FlowInfo flowInfo,
     MethodBinding methodBinding,
     Expression[] arguments) {
   // compare actual null-status against parameter annotations of the called method:
   if (arguments != null) {
     CompilerOptions compilerOptions = currentScope.compilerOptions();
     boolean considerTypeAnnotations =
         compilerOptions.sourceLevel >= ClassFileConstants.JDK1_8
             && compilerOptions.isAnnotationBasedNullAnalysisEnabled;
     boolean hasJDK15NullAnnotations = methodBinding.parameterNonNullness != null;
     int numParamsToCheck = methodBinding.parameters.length;
     if (considerTypeAnnotations || hasJDK15NullAnnotations) {
       // check if varargs need special treatment:
       boolean passThrough = false;
       if (methodBinding.isVarargs()) {
         int varArgPos = numParamsToCheck - 1;
         // this if-block essentially copied from generateArguments(..):
         if (numParamsToCheck == arguments.length) {
           TypeBinding varArgsType = methodBinding.parameters[varArgPos];
           TypeBinding lastType = arguments[varArgPos].resolvedType;
           if (lastType == TypeBinding.NULL
               || (varArgsType.dimensions() == lastType.dimensions()
                   && lastType.isCompatibleWith(varArgsType)))
             passThrough = true; // pass directly as-is
         }
         if (!passThrough)
           numParamsToCheck--; // with non-passthrough varargs last param is fed from individual
         // args -> don't check
       }
     }
     if (considerTypeAnnotations) {
       for (int i = 0; i < numParamsToCheck; i++) {
         TypeBinding expectedType = methodBinding.parameters[i];
         Expression argument = arguments[i];
         // prefer check based on type annotations:
         int severity = findNullTypeAnnotationMismatch(expectedType, argument.resolvedType);
         if (severity > 0) {
           // immediate reporting:
           currentScope
               .problemReporter()
               .nullityMismatchingTypeAnnotation(
                   argument,
                   argument.resolvedType,
                   expectedType,
                   severity == 1,
                   currentScope.environment());
           // next check flow-based null status against null JDK15-style annotations:
         } else if (hasJDK15NullAnnotations
             && methodBinding.parameterNonNullness[i] == Boolean.TRUE) {
           int nullStatus =
               argument.nullStatus(
                   flowInfo,
                   flowContext); // slight loss of precision: should also use the null info from
           // the receiver.
           if (nullStatus != FlowInfo.NON_NULL) // if required non-null is not provided
           flowContext.recordNullityMismatch(
                 currentScope, argument, argument.resolvedType, expectedType, nullStatus);
         }
       }
     } else if (hasJDK15NullAnnotations) {
       for (int i = 0; i < numParamsToCheck; i++) {
         if (methodBinding.parameterNonNullness[i] == Boolean.TRUE) {
           TypeBinding expectedType = methodBinding.parameters[i];
           Expression argument = arguments[i];
           int nullStatus =
               argument.nullStatus(
                   flowInfo,
                   flowContext); // slight loss of precision: should also use the null info from
           // the receiver.
           if (nullStatus != FlowInfo.NON_NULL) // if required non-null is not provided
           flowContext.recordNullityMismatch(
                 currentScope, argument, argument.resolvedType, expectedType, nullStatus);
         }
       }
     }
   }
 }
  public TypeBinding resolveType(BlockScope scope) {
    // Propagate the type checking to the arguments, and check if the constructor is defined.
    this.constant = Constant.NotAConstant;
    if (this.type == null) {
      // initialization of an enum constant
      this.resolvedType = scope.enclosingReceiverType();
    } else {
      this.resolvedType = this.type.resolveType(scope, true /* check bounds*/);
      checkParameterizedAllocation:
      {
        if (this.type
            instanceof ParameterizedQualifiedTypeReference) { // disallow new X<String>.Y<Integer>()
          ReferenceBinding currentType = (ReferenceBinding) this.resolvedType;
          if (currentType == null) return currentType;
          do {
            // isStatic() is answering true for toplevel types
            if ((currentType.modifiers & ClassFileConstants.AccStatic) != 0)
              break checkParameterizedAllocation;
            if (currentType.isRawType()) break checkParameterizedAllocation;
          } while ((currentType = currentType.enclosingType()) != null);
          ParameterizedQualifiedTypeReference qRef =
              (ParameterizedQualifiedTypeReference) this.type;
          for (int i = qRef.typeArguments.length - 2; i >= 0; i--) {
            if (qRef.typeArguments[i] != null) {
              scope
                  .problemReporter()
                  .illegalQualifiedParameterizedTypeAllocation(this.type, this.resolvedType);
              break;
            }
          }
        }
      }
    }
    // will check for null after args are resolved

    final boolean isDiamond = this.type != null && (this.type.bits & ASTNode.IsDiamond) != 0;
    // resolve type arguments (for generic constructor call)
    if (this.typeArguments != null) {
      int length = this.typeArguments.length;
      boolean argHasError = scope.compilerOptions().sourceLevel < ClassFileConstants.JDK1_5;
      this.genericTypeArguments = new TypeBinding[length];
      for (int i = 0; i < length; i++) {
        TypeReference typeReference = this.typeArguments[i];
        if ((this.genericTypeArguments[i] =
                typeReference.resolveType(scope, true /* check bounds*/))
            == null) {
          argHasError = true;
        }
        if (argHasError && typeReference instanceof Wildcard) {
          scope.problemReporter().illegalUsageOfWildcard(typeReference);
        }
      }
      if (isDiamond) {
        scope.problemReporter().diamondNotWithExplicitTypeArguments(this.typeArguments);
        return null;
      }
      if (argHasError) {
        if (this.arguments != null) { // still attempt to resolve arguments
          for (int i = 0, max = this.arguments.length; i < max; i++) {
            this.arguments[i].resolveType(scope);
          }
        }
        return null;
      }
    }

    // buffering the arguments' types
    boolean argsContainCast = false;
    TypeBinding[] argumentTypes = Binding.NO_PARAMETERS;
    if (this.arguments != null) {
      boolean argHasError = false;
      int length = this.arguments.length;
      argumentTypes = new TypeBinding[length];
      for (int i = 0; i < length; i++) {
        Expression argument = this.arguments[i];
        if (argument instanceof CastExpression) {
          argument.bits |= DisableUnnecessaryCastCheck; // will check later on
          argsContainCast = true;
        }
        if ((argumentTypes[i] = argument.resolveType(scope)) == null) {
          argHasError = true;
        }
      }
      if (argHasError) {
        /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=345359, if arguments have errors, completely bail out in the <> case.
          No meaningful type resolution is possible since inference of the elided types is fully tied to argument types. Do
          not return the partially resolved type.
        */
        if (isDiamond) {
          return null; // not the partially cooked this.resolvedType
        }
        if (this.resolvedType instanceof ReferenceBinding) {
          // record a best guess, for clients who need hint about possible constructor match
          TypeBinding[] pseudoArgs = new TypeBinding[length];
          for (int i = length; --i >= 0; ) {
            pseudoArgs[i] =
                argumentTypes[i] == null
                    ? TypeBinding.NULL
                    : argumentTypes[i]; // replace args with errors with null type
          }
          this.binding =
              scope.findMethod(
                  (ReferenceBinding) this.resolvedType, TypeConstants.INIT, pseudoArgs, this);
          if (this.binding != null && !this.binding.isValidBinding()) {
            MethodBinding closestMatch = ((ProblemMethodBinding) this.binding).closestMatch;
            // record the closest match, for clients who may still need hint about possible method
            // match
            if (closestMatch != null) {
              if (closestMatch.original().typeVariables
                  != Binding.NO_TYPE_VARIABLES) { // generic method
                // shouldn't return generic method outside its context, rather convert it to raw
                // method (175409)
                closestMatch =
                    scope
                        .environment()
                        .createParameterizedGenericMethod(
                            closestMatch.original(), (RawTypeBinding) null);
              }
              this.binding = closestMatch;
              MethodBinding closestMatchOriginal = closestMatch.original();
              if (closestMatchOriginal.isOrEnclosedByPrivateType()
                  && !scope.isDefinedInMethod(closestMatchOriginal)) {
                // ignore cases where method is used from within inside itself (e.g. direct
                // recursions)
                closestMatchOriginal.modifiers |= ExtraCompilerModifiers.AccLocallyUsed;
              }
            }
          }
        }
        return this.resolvedType;
      }
    }
    if (this.resolvedType == null || !this.resolvedType.isValidBinding()) {
      return null;
    }

    // null type denotes fake allocation for enum constant inits
    if (this.type != null && !this.resolvedType.canBeInstantiated()) {
      scope.problemReporter().cannotInstantiate(this.type, this.resolvedType);
      return this.resolvedType;
    }
    if (isDiamond) {
      TypeBinding[] inferredTypes =
          inferElidedTypes(
              ((ParameterizedTypeBinding) this.resolvedType).genericType(),
              null,
              argumentTypes,
              scope);
      if (inferredTypes == null) {
        scope.problemReporter().cannotInferElidedTypes(this);
        return this.resolvedType = null;
      }
      this.resolvedType =
          this.type.resolvedType =
              scope
                  .environment()
                  .createParameterizedType(
                      ((ParameterizedTypeBinding) this.resolvedType).genericType(),
                      inferredTypes,
                      ((ParameterizedTypeBinding) this.resolvedType).enclosingType());
    }
    ReferenceBinding allocationType = (ReferenceBinding) this.resolvedType;
    if (!(this.binding = scope.getConstructor(allocationType, argumentTypes, this))
        .isValidBinding()) {
      if (this.binding.declaringClass == null) {
        this.binding.declaringClass = allocationType;
      }
      if (this.type != null && !this.type.resolvedType.isValidBinding()) {
        return null;
      }
      scope.problemReporter().invalidConstructor(this, this.binding);
      return this.resolvedType;
    }
    if ((this.binding.tagBits & TagBits.HasMissingType) != 0) {
      scope.problemReporter().missingTypeInConstructor(this, this.binding);
    }
    if (isMethodUseDeprecated(this.binding, scope, true))
      scope.problemReporter().deprecatedMethod(this.binding, this);
    if (checkInvocationArguments(
        scope,
        null,
        allocationType,
        this.binding,
        this.arguments,
        argumentTypes,
        argsContainCast,
        this)) {
      this.bits |= ASTNode.Unchecked;
    }
    if (this.typeArguments != null
        && this.binding.original().typeVariables == Binding.NO_TYPE_VARIABLES) {
      scope
          .problemReporter()
          .unnecessaryTypeArgumentsForMethodInvocation(
              this.binding, this.genericTypeArguments, this.typeArguments);
    }
    if (!isDiamond && this.resolvedType.isParameterizedTypeWithActualArguments()) {
      checkTypeArgumentRedundancy(
          (ParameterizedTypeBinding) this.resolvedType, null, argumentTypes, scope);
    }
    final CompilerOptions compilerOptions = scope.compilerOptions();
    if (compilerOptions.isAnnotationBasedNullAnalysisEnabled
        && (this.binding.tagBits & TagBits.IsNullnessKnown) == 0) {
      new ImplicitNullAnnotationVerifier(
              scope.environment(), compilerOptions.inheritNullAnnotations)
          .checkImplicitNullAnnotations(this.binding, null /*srcMethod*/, false, scope);
    }
    return allocationType;
  }
示例#23
0
  public static boolean checkInvocationArguments(
      BlockScope scope,
      Expression receiver,
      TypeBinding receiverType,
      MethodBinding method,
      Expression[] arguments,
      TypeBinding[] argumentTypes,
      boolean argsContainCast,
      InvocationSite invocationSite) {
    TypeBinding[] params = method.parameters;
    int paramLength = params.length;
    boolean isRawMemberInvocation =
        !method.isStatic()
            && !receiverType.isUnboundWildcard()
            && method.declaringClass.isRawType()
            && method.hasSubstitutedParameters();

    boolean uncheckedBoundCheck =
        (method.tagBits & TagBits.HasUncheckedTypeArgumentForBoundCheck) != 0;
    MethodBinding rawOriginalGenericMethod = null;
    if (!isRawMemberInvocation) {
      if (method instanceof ParameterizedGenericMethodBinding) {
        ParameterizedGenericMethodBinding paramMethod = (ParameterizedGenericMethodBinding) method;
        if (paramMethod.isRaw && method.hasSubstitutedParameters()) {
          rawOriginalGenericMethod = method.original();
        }
      }
    }
    int invocationStatus = INVOCATION_ARGUMENT_OK;
    if (arguments == null) {
      if (method.isVarargs()) {
        TypeBinding parameterType =
            ((ArrayBinding) params[paramLength - 1])
                .elementsType(); // no element was supplied for vararg parameter
        if (!parameterType.isReifiable()) {
          scope
              .problemReporter()
              .unsafeGenericArrayForVarargs(parameterType, (ASTNode) invocationSite);
        }
      }
    } else {
      if (method.isVarargs()) {
        // 4 possibilities exist for a call to the vararg method foo(int i, long ... value) :
        // foo(1), foo(1, 2), foo(1, 2, 3, 4) & foo(1, new long[] {1, 2})
        int lastIndex = paramLength - 1;
        for (int i = 0; i < lastIndex; i++) {
          TypeBinding originalRawParam =
              rawOriginalGenericMethod == null ? null : rawOriginalGenericMethod.parameters[i];
          invocationStatus |=
              checkInvocationArgument(
                  scope, arguments[i], params[i], argumentTypes[i], originalRawParam);
        }
        int argLength = arguments.length;
        if (lastIndex < argLength) { // vararg argument was provided
          TypeBinding parameterType = params[lastIndex];
          TypeBinding originalRawParam = null;

          if (paramLength != argLength
              || parameterType.dimensions() != argumentTypes[lastIndex].dimensions()) {
            parameterType =
                ((ArrayBinding) parameterType)
                    .elementsType(); // single element was provided for vararg parameter
            if (!parameterType.isReifiable()) {
              scope
                  .problemReporter()
                  .unsafeGenericArrayForVarargs(parameterType, (ASTNode) invocationSite);
            }
            originalRawParam =
                rawOriginalGenericMethod == null
                    ? null
                    : ((ArrayBinding) rawOriginalGenericMethod.parameters[lastIndex])
                        .elementsType();
          }
          for (int i = lastIndex; i < argLength; i++) {
            invocationStatus |=
                checkInvocationArgument(
                    scope, arguments[i], parameterType, argumentTypes[i], originalRawParam);
          }
        }
        if (paramLength == argLength) { // 70056
          int varargsIndex = paramLength - 1;
          ArrayBinding varargsType = (ArrayBinding) params[varargsIndex];
          TypeBinding lastArgType = argumentTypes[varargsIndex];
          int dimensions;
          if (lastArgType == TypeBinding.NULL) {
            if (!(varargsType.leafComponentType().isBaseType() && varargsType.dimensions() == 1))
              scope.problemReporter().varargsArgumentNeedCast(method, lastArgType, invocationSite);
          } else if (varargsType.dimensions <= (dimensions = lastArgType.dimensions())) {
            if (lastArgType.leafComponentType().isBaseType()) {
              dimensions--;
            }
            if (varargsType.dimensions < dimensions) {
              scope.problemReporter().varargsArgumentNeedCast(method, lastArgType, invocationSite);
            } else if (varargsType.dimensions == dimensions
                && lastArgType != varargsType
                && lastArgType.leafComponentType().erasure()
                    != varargsType.leafComponentType.erasure()
                && lastArgType.isCompatibleWith(varargsType.elementsType())
                && lastArgType.isCompatibleWith(varargsType)) {
              scope.problemReporter().varargsArgumentNeedCast(method, lastArgType, invocationSite);
            }
          }
        }
      } else {
        for (int i = 0; i < paramLength; i++) {
          TypeBinding originalRawParam =
              rawOriginalGenericMethod == null ? null : rawOriginalGenericMethod.parameters[i];
          invocationStatus |=
              checkInvocationArgument(
                  scope, arguments[i], params[i], argumentTypes[i], originalRawParam);
        }
      }
      if (argsContainCast) {
        CastExpression.checkNeedForArgumentCasts(
            scope, receiver, receiverType, method, arguments, argumentTypes, invocationSite);
      }
    }
    if ((invocationStatus & INVOCATION_ARGUMENT_WILDCARD) != 0) {
      scope
          .problemReporter()
          .wildcardInvocation((ASTNode) invocationSite, receiverType, method, argumentTypes);
    } else if (!method.isStatic()
        && !receiverType.isUnboundWildcard()
        && method.declaringClass.isRawType()
        && method.hasSubstitutedParameters()) {
      scope.problemReporter().unsafeRawInvocation((ASTNode) invocationSite, method);
    } else if (rawOriginalGenericMethod != null
        || uncheckedBoundCheck
        || ((invocationStatus & INVOCATION_ARGUMENT_UNCHECKED) != 0
            && method instanceof ParameterizedGenericMethodBinding
        /*&& method.returnType != scope.environment().convertToRawType(method.returnType.erasure(), true)*/ )) {
      scope
          .problemReporter()
          .unsafeRawGenericMethodInvocation((ASTNode) invocationSite, method, argumentTypes);
      return true;
    }
    return false;
  }
  public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {

    Constant cst = this.left.optimizedBooleanConstant();
    boolean isLeftOptimizedTrue = cst != Constant.NotAConstant && cst.booleanValue() == true;
    boolean isLeftOptimizedFalse = cst != Constant.NotAConstant && cst.booleanValue() == false;

    if (isLeftOptimizedFalse) {
      // FALSE || anything
      // need to be careful of scenario:
      //		(x || y) || !z, if passing the left info to the right, it would be swapped by the !
      FlowInfo mergedInfo =
          this.left.analyseCode(currentScope, flowContext, flowInfo).unconditionalInits();
      flowContext.expireNullCheckedFieldInfo();
      mergedInfo = this.right.analyseCode(currentScope, flowContext, mergedInfo);
      flowContext.expireNullCheckedFieldInfo();
      this.mergedInitStateIndex = currentScope.methodScope().recordInitializationStates(mergedInfo);
      return mergedInfo;
    }

    FlowInfo leftInfo = this.left.analyseCode(currentScope, flowContext, flowInfo);
    flowContext.expireNullCheckedFieldInfo();

    // need to be careful of scenario:
    //		(x || y) || !z, if passing the left info to the right, it would be swapped by the !
    FlowInfo rightInfo = leftInfo.initsWhenFalse().unconditionalCopy();
    this.rightInitStateIndex = currentScope.methodScope().recordInitializationStates(rightInfo);

    int previousMode = rightInfo.reachMode();
    if (isLeftOptimizedTrue) {
      if ((rightInfo.reachMode() & FlowInfo.UNREACHABLE) == 0) {
        currentScope.problemReporter().fakeReachable(this.right);
        rightInfo.setReachMode(FlowInfo.UNREACHABLE_OR_DEAD);
      }
    }
    rightInfo = this.right.analyseCode(currentScope, flowContext, rightInfo);
    flowContext.expireNullCheckedFieldInfo();
    if ((this.left.implicitConversion & TypeIds.UNBOXING) != 0) {
      this.left.checkNPE(currentScope, flowContext, flowInfo);
    }
    if ((this.right.implicitConversion & TypeIds.UNBOXING) != 0) {
      this.right.checkNPE(currentScope, flowContext, flowInfo);
    }
    // The definitely null variables in right info when true should not be missed out while merging
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=299900
    FlowInfo leftInfoWhenTrueForMerging =
        leftInfo
            .initsWhenTrue()
            .unconditionalCopy()
            .addPotentialInitializationsFrom(rightInfo.unconditionalInitsWithoutSideEffect());
    FlowInfo mergedInfo =
        FlowInfo.conditional(
            // merging two true initInfos for such a negative case: if ((t && (b = t)) || f) r = b;
            // // b may not have been initialized
            leftInfoWhenTrueForMerging
                .unconditionalInits()
                .mergedWith(
                    rightInfo.safeInitsWhenTrue().setReachMode(previousMode).unconditionalInits()),
            rightInfo.initsWhenFalse());
    this.mergedInitStateIndex = currentScope.methodScope().recordInitializationStates(mergedInfo);
    return mergedInfo;
  }
  public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
    // check captured variables are initialized in current context (26134)
    checkCapturedLocalInitializationIfNecessary(
        (ReferenceBinding) this.binding.declaringClass.erasure(), currentScope, flowInfo);

    // process arguments
    if (this.arguments != null) {
      boolean analyseResources = currentScope.compilerOptions().analyseResourceLeaks;
      boolean hasResourceWrapperType =
          analyseResources
              && this.resolvedType instanceof ReferenceBinding
              && ((ReferenceBinding) this.resolvedType).hasTypeBit(TypeIds.BitWrapperCloseable);
      for (int i = 0, count = this.arguments.length; i < count; i++) {
        flowInfo =
            this.arguments[i].analyseCode(currentScope, flowContext, flowInfo).unconditionalInits();
        // if argument is an AutoCloseable insert info that it *may* be closed (by the target
        // method, i.e.)
        if (analyseResources
            && !hasResourceWrapperType) { // allocation of wrapped closeables is analyzed specially
          flowInfo =
              FakedTrackingVariable.markPassedToOutside(
                  currentScope, this.arguments[i], flowInfo, flowContext, false);
        }
        this.arguments[i].checkNPEbyUnboxing(currentScope, flowContext, flowInfo);
      }
      analyseArguments(currentScope, flowContext, flowInfo, this.binding, this.arguments);
    }

    // record some dependency information for exception types
    ReferenceBinding[] thrownExceptions;
    if (((thrownExceptions = this.binding.thrownExceptions).length) != 0) {
      if ((this.bits & ASTNode.Unchecked) != 0 && this.genericTypeArguments == null) {
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=277643, align with javac on JLS 15.12.2.6
        thrownExceptions =
            currentScope.environment().convertToRawTypes(this.binding.thrownExceptions, true, true);
      }
      // check exception handling
      flowContext.checkExceptionHandlers(
          thrownExceptions, this, flowInfo.unconditionalCopy(), currentScope);
    }

    // after having analysed exceptions above start tracking newly allocated resource:
    if (currentScope.compilerOptions().analyseResourceLeaks
        && FakedTrackingVariable.isAnyCloseable(this.resolvedType))
      FakedTrackingVariable.analyseCloseableAllocation(currentScope, flowInfo, this);

    if (this.binding.declaringClass.isMemberType() && !this.binding.declaringClass.isStatic()) {
      // allocating a non-static member type without an enclosing instance of parent type
      // https://bugs.eclipse.org/bugs/show_bug.cgi?id=335845
      currentScope.resetDeclaringClassMethodStaticFlag(this.binding.declaringClass.enclosingType());
      // Reviewed for https://bugs.eclipse.org/bugs/show_bug.cgi?id=378674 :
      // The corresponding problem (when called from static) is not produced until during code
      // generation
    }
    manageEnclosingInstanceAccessIfNecessary(currentScope, flowInfo);
    manageSyntheticAccessIfNecessary(currentScope, flowInfo);

    // account for possible exceptions thrown by the constructor
    flowContext.recordAbruptExit(); // TODO whitelist of ctors that cannot throw any exc.??

    return flowInfo;
  }
  public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
    this.preAssertInitStateIndex = currentScope.methodScope().recordInitializationStates(flowInfo);

    Constant cst = this.assertExpression.optimizedBooleanConstant();
    if ((this.assertExpression.implicitConversion & TypeIds.UNBOXING) != 0) {
      this.assertExpression.checkNPE(currentScope, flowContext, flowInfo);
    }
    boolean isOptimizedTrueAssertion = cst != Constant.NotAConstant && cst.booleanValue() == true;
    boolean isOptimizedFalseAssertion = cst != Constant.NotAConstant && cst.booleanValue() == false;

    flowContext.tagBits |= FlowContext.HIDE_NULL_COMPARISON_WARNING;
    FlowInfo conditionFlowInfo =
        this.assertExpression.analyseCode(currentScope, flowContext, flowInfo.copy());
    flowContext.extendTimeToLiveForNullCheckedField(1); // survive this assert as a Statement
    flowContext.tagBits &= ~FlowContext.HIDE_NULL_COMPARISON_WARNING;
    UnconditionalFlowInfo assertWhenTrueInfo =
        conditionFlowInfo.initsWhenTrue().unconditionalInits();
    FlowInfo assertInfo = conditionFlowInfo.initsWhenFalse();
    if (isOptimizedTrueAssertion) {
      assertInfo.setReachMode(FlowInfo.UNREACHABLE_OR_DEAD);
    }

    if (this.exceptionArgument != null) {
      // only gets evaluated when escaping - results are not taken into account
      FlowInfo exceptionInfo =
          this.exceptionArgument.analyseCode(currentScope, flowContext, assertInfo.copy());

      if (isOptimizedTrueAssertion) {
        currentScope.problemReporter().fakeReachable(this.exceptionArgument);
      } else {
        flowContext.checkExceptionHandlers(
            currentScope.getJavaLangAssertionError(), this, exceptionInfo, currentScope);
      }
    }

    if (!isOptimizedTrueAssertion) {
      // add the assert support in the clinit
      manageSyntheticAccessIfNecessary(currentScope, flowInfo);
    }
    // account for potential AssertionError:
    flowContext.recordAbruptExit();
    if (isOptimizedFalseAssertion) {
      return flowInfo; // if assertions are enabled, the following code will be unreachable
      // change this if we need to carry null analysis results of the assert
      // expression downstream
    } else {
      CompilerOptions compilerOptions = currentScope.compilerOptions();
      if (!compilerOptions.includeNullInfoFromAsserts) {
        // keep just the initializations info, don't include assert's null info
        // merge initialization info's and then add back the null info from flowInfo to
        // make sure that the empty null info of assertInfo doesnt change flowInfo's null info.
        return ((flowInfo.nullInfoLessUnconditionalCopy())
                .mergedWith(assertInfo.nullInfoLessUnconditionalCopy()))
            .addNullInfoFrom(flowInfo);
      }
      return flowInfo
          .mergedWith(assertInfo.nullInfoLessUnconditionalCopy())
          .addInitializationsFrom(assertWhenTrueInfo.discardInitializationInfo());
      // keep the merge from the initial code for the definite assignment
      // analysis, tweak the null part to influence nulls downstream
    }
  }
示例#27
0
 /**
  * Resolve annotations, and check duplicates, answers combined tagBits for recognized standard
  * annotations
  */
 public static void resolveAnnotations(
     BlockScope scope, Annotation[] sourceAnnotations, Binding recipient) {
   AnnotationBinding[] annotations = null;
   int length = sourceAnnotations == null ? 0 : sourceAnnotations.length;
   if (recipient != null) {
     switch (recipient.kind()) {
       case Binding.PACKAGE:
         PackageBinding packageBinding = (PackageBinding) recipient;
         if ((packageBinding.tagBits & TagBits.AnnotationResolved) != 0) return;
         packageBinding.tagBits |=
             (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
         break;
       case Binding.TYPE:
       case Binding.GENERIC_TYPE:
         ReferenceBinding type = (ReferenceBinding) recipient;
         if ((type.tagBits & TagBits.AnnotationResolved) != 0) return;
         type.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
         if (length > 0) {
           annotations = new AnnotationBinding[length];
           type.setAnnotations(annotations);
         }
         break;
       case Binding.METHOD:
         MethodBinding method = (MethodBinding) recipient;
         if ((method.tagBits & TagBits.AnnotationResolved) != 0) return;
         method.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
         if (length > 0) {
           annotations = new AnnotationBinding[length];
           method.setAnnotations(annotations);
         }
         break;
       case Binding.FIELD:
         FieldBinding field = (FieldBinding) recipient;
         if ((field.tagBits & TagBits.AnnotationResolved) != 0) return;
         field.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
         if (length > 0) {
           annotations = new AnnotationBinding[length];
           field.setAnnotations(annotations);
         }
         break;
       case Binding.LOCAL:
         LocalVariableBinding local = (LocalVariableBinding) recipient;
         if ((local.tagBits & TagBits.AnnotationResolved) != 0) return;
         local.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
         if (length > 0) {
           annotations = new AnnotationBinding[length];
           local.setAnnotations(annotations);
         }
         break;
       default:
         return;
     }
   }
   if (sourceAnnotations == null) return;
   for (int i = 0; i < length; i++) {
     Annotation annotation = sourceAnnotations[i];
     final Binding annotationRecipient = annotation.recipient;
     if (annotationRecipient != null && recipient != null) {
       // only local and field can share annnotations
       switch (recipient.kind()) {
         case Binding.FIELD:
           FieldBinding field = (FieldBinding) recipient;
           field.tagBits = ((FieldBinding) annotationRecipient).tagBits;
           break;
         case Binding.LOCAL:
           LocalVariableBinding local = (LocalVariableBinding) recipient;
           local.tagBits = ((LocalVariableBinding) annotationRecipient).tagBits;
           break;
       }
       if (annotations != null) {
         // need to fill the instances array
         annotations[0] = annotation.getCompilerAnnotation();
         for (int j = 1; j < length; j++) {
           Annotation annot = sourceAnnotations[j];
           annotations[j] = annot.getCompilerAnnotation();
         }
       }
       return;
     } else {
       annotation.recipient = recipient;
       annotation.resolveType(scope);
       // null if receiver is a package binding
       if (annotations != null) {
         annotations[i] = annotation.getCompilerAnnotation();
       }
     }
   }
   // check duplicate annotations
   if (annotations != null) {
     AnnotationBinding[] distinctAnnotations =
         annotations; // only copy after 1st duplicate is detected
     for (int i = 0; i < length; i++) {
       AnnotationBinding annotation = distinctAnnotations[i];
       if (annotation == null) continue;
       TypeBinding annotationType = annotation.getAnnotationType();
       boolean foundDuplicate = false;
       for (int j = i + 1; j < length; j++) {
         AnnotationBinding otherAnnotation = distinctAnnotations[j];
         if (otherAnnotation == null) continue;
         if (otherAnnotation.getAnnotationType() == annotationType) {
           foundDuplicate = true;
           if (distinctAnnotations == annotations) {
             System.arraycopy(
                 distinctAnnotations,
                 0,
                 distinctAnnotations = new AnnotationBinding[length],
                 0,
                 length);
           }
           distinctAnnotations[j] = null; // report it only once
           scope.problemReporter().duplicateAnnotation(sourceAnnotations[j]);
         }
       }
       if (foundDuplicate) {
         scope.problemReporter().duplicateAnnotation(sourceAnnotations[i]);
       }
     }
   }
 }
示例#28
0
  public void resolve(BlockScope upperScope) {

    // special scope for secret locals optimization.
    this.scope = new BlockScope(upperScope);

    BlockScope tryScope = new BlockScope(scope);
    BlockScope finallyScope = null;

    if (finallyBlock != null) {
      if (finallyBlock.isEmptyBlock()) {
        if ((finallyBlock.bits & UndocumentedEmptyBlockMASK) != 0) {
          scope
              .problemReporter()
              .undocumentedEmptyBlock(finallyBlock.sourceStart, finallyBlock.sourceEnd);
        }
      } else {
        finallyScope = new BlockScope(scope, false); // don't add it yet to parent scope

        // provision for returning and forcing the finally block to run
        MethodScope methodScope = scope.methodScope();

        // the type does not matter as long as it is not a base type
        if (!upperScope.compilerOptions().inlineJsrBytecode) {
          this.returnAddressVariable =
              new LocalVariableBinding(
                  SecretReturnName, upperScope.getJavaLangObject(), AccDefault, false);
          finallyScope.addLocalVariable(returnAddressVariable);
          this.returnAddressVariable.setConstant(NotAConstant); // not inlinable
        }
        this.subRoutineStartLabel = new Label();

        this.anyExceptionVariable =
            new LocalVariableBinding(
                SecretAnyHandlerName, scope.getJavaLangThrowable(), AccDefault, false);
        finallyScope.addLocalVariable(this.anyExceptionVariable);
        this.anyExceptionVariable.setConstant(NotAConstant); // not inlinable

        if (!methodScope.isInsideInitializer()) {
          MethodBinding methodBinding =
              ((AbstractMethodDeclaration) methodScope.referenceContext).binding;
          if (methodBinding != null) {
            TypeBinding methodReturnType = methodBinding.returnType;
            if (methodReturnType.id != T_void) {
              this.secretReturnValue =
                  new LocalVariableBinding(
                      SecretLocalDeclarationName, methodReturnType, AccDefault, false);
              finallyScope.addLocalVariable(this.secretReturnValue);
              this.secretReturnValue.setConstant(NotAConstant); // not inlinable
            }
          }
        }
        finallyBlock.resolveUsing(finallyScope);
        // force the finally scope to have variable positions shifted after its try scope and catch
        // ones
        finallyScope.shiftScopes =
            new BlockScope[catchArguments == null ? 1 : catchArguments.length + 1];
        finallyScope.shiftScopes[0] = tryScope;
      }
    }
    this.tryBlock.resolveUsing(tryScope);

    // arguments type are checked against JavaLangThrowable in resolveForCatch(..)
    if (this.catchBlocks != null) {
      int length = this.catchArguments.length;
      TypeBinding[] argumentTypes = new TypeBinding[length];
      boolean catchHasError = false;
      for (int i = 0; i < length; i++) {
        BlockScope catchScope = new BlockScope(scope);
        if (finallyScope != null) {
          finallyScope.shiftScopes[i + 1] = catchScope;
        }
        // side effect on catchScope in resolveForCatch(..)
        if ((argumentTypes[i] = catchArguments[i].resolveForCatch(catchScope)) == null) {
          catchHasError = true;
        }
        catchBlocks[i].resolveUsing(catchScope);
      }
      if (catchHasError) {
        return;
      }
      // Verify that the catch clause are ordered in the right way:
      // more specialized first.
      this.caughtExceptionTypes = new ReferenceBinding[length];
      for (int i = 0; i < length; i++) {
        caughtExceptionTypes[i] = (ReferenceBinding) argumentTypes[i];
        for (int j = 0; j < i; j++) {
          if (caughtExceptionTypes[i].isCompatibleWith(argumentTypes[j])) {
            scope
                .problemReporter()
                .wrongSequenceOfExceptionTypesError(
                    this, caughtExceptionTypes[i], i, argumentTypes[j]);
          }
        }
      }
    } else {
      caughtExceptionTypes = new ReferenceBinding[0];
    }

    if (finallyScope != null) {
      // add finallyScope as last subscope, so it can be shifted behind try/catch subscopes.
      // the shifting is necessary to achieve no overlay in between the finally scope and its
      // sibling in term of local variable positions.
      this.scope.addSubscope(finallyScope);
    }
  }
示例#29
0
  public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {

    // Consider the try block and catch block so as to compute the intersection of initializations
    // and
    // the minimum exit relative depth amongst all of them. Then consider the subroutine, and append
    // its
    // initialization to the try/catch ones, if the subroutine completes normally. If the subroutine
    // does not
    // complete, then only keep this result for the rest of the analysis

    // process the finally block (subroutine) - create a context for the subroutine

    preTryInitStateIndex = currentScope.methodScope().recordInitializationStates(flowInfo);

    if (anyExceptionVariable != null) {
      anyExceptionVariable.useFlag = LocalVariableBinding.USED;
    }
    if (returnAddressVariable != null) { // TODO (philippe) if subroutine is escaping, unused
      returnAddressVariable.useFlag = LocalVariableBinding.USED;
    }
    InsideSubRoutineFlowContext insideSubContext;
    FinallyFlowContext finallyContext;
    UnconditionalFlowInfo subInfo;
    if (subRoutineStartLabel == null) {
      // no finally block
      insideSubContext = null;
      finallyContext = null;
      subInfo = null;
    } else {
      // analyse finally block first
      insideSubContext = new InsideSubRoutineFlowContext(flowContext, this);
      subInfo =
          finallyBlock
              .analyseCode(
                  currentScope,
                  finallyContext = new FinallyFlowContext(flowContext, finallyBlock),
                  flowInfo.copy().unconditionalInits().discardNullRelatedInitializations())
              .unconditionalInits();
      if (subInfo == FlowInfo.DEAD_END) {
        isSubRoutineEscaping = true;
        scope.problemReporter().finallyMustCompleteNormally(finallyBlock);
      }
      this.subRoutineInits = subInfo;
    }
    // process the try block in a context handling the local exceptions.
    ExceptionHandlingFlowContext handlingContext =
        new ExceptionHandlingFlowContext(
            insideSubContext == null ? flowContext : insideSubContext,
            tryBlock,
            caughtExceptionTypes,
            scope,
            flowInfo.unconditionalInits());

    FlowInfo tryInfo;
    if (tryBlock.isEmptyBlock()) {
      tryInfo = flowInfo;
      tryBlockExit = false;
    } else {
      tryInfo = tryBlock.analyseCode(currentScope, handlingContext, flowInfo.copy());
      tryBlockExit = !tryInfo.isReachable();
    }

    // check unreachable catch blocks
    handlingContext.complainIfUnusedExceptionHandlers(scope, this);

    // process the catch blocks - computing the minimal exit depth amongst try/catch
    if (catchArguments != null) {
      int catchCount;
      catchExits = new boolean[catchCount = catchBlocks.length];
      for (int i = 0; i < catchCount; i++) {
        // keep track of the inits that could potentially have led to this exception handler (for
        // final assignments diagnosis)
        FlowInfo catchInfo =
            flowInfo
                .copy()
                .unconditionalInits()
                .addPotentialInitializationsFrom(
                    handlingContext.initsOnException(caughtExceptionTypes[i]).unconditionalInits())
                .addPotentialInitializationsFrom(tryInfo.unconditionalInits())
                .addPotentialInitializationsFrom(handlingContext.initsOnReturn);

        // catch var is always set
        LocalVariableBinding catchArg = catchArguments[i].binding;
        FlowContext catchContext = insideSubContext == null ? flowContext : insideSubContext;
        catchInfo.markAsDefinitelyAssigned(catchArg);
        catchInfo.markAsDefinitelyNonNull(catchArg);
        /*
        "If we are about to consider an unchecked exception handler, potential inits may have occured inside
        the try block that need to be detected , e.g.
        try { x = 1; throwSomething();} catch(Exception e){ x = 2} "
        "(uncheckedExceptionTypes notNil and: [uncheckedExceptionTypes at: index])
        ifTrue: [catchInits addPotentialInitializationsFrom: tryInits]."
        */
        if (tryBlock.statements == null) {
          catchInfo.setReachMode(FlowInfo.UNREACHABLE);
        }
        catchInfo = catchBlocks[i].analyseCode(currentScope, catchContext, catchInfo);
        catchExits[i] = !catchInfo.isReachable();
        tryInfo = tryInfo.mergedWith(catchInfo.unconditionalInits());
      }
    }
    if (subRoutineStartLabel == null) {
      mergedInitStateIndex = currentScope.methodScope().recordInitializationStates(tryInfo);
      return tryInfo;
    }

    // we also need to check potential multiple assignments of final variables inside the finally
    // block
    // need to include potential inits from returns inside the try/catch parts - 1GK2AOF
    finallyContext.complainOnDeferredChecks(
        tryInfo.isReachable()
            ? (tryInfo.addPotentialInitializationsFrom(insideSubContext.initsOnReturn))
            : insideSubContext.initsOnReturn,
        currentScope);
    if (subInfo == FlowInfo.DEAD_END) {
      mergedInitStateIndex = currentScope.methodScope().recordInitializationStates(subInfo);
      return subInfo;
    } else {
      FlowInfo mergedInfo = tryInfo.addInitializationsFrom(subInfo);
      mergedInitStateIndex = currentScope.methodScope().recordInitializationStates(mergedInfo);
      return mergedInfo;
    }
  }
  public TypeBinding resolveType(BlockScope scope) {

    // Build an array type reference using the current dimensions
    // The parser does not check for the fact that dimension may be null
    // only at the -end- like new int [4][][]. The parser allows new int[][4][]
    // so this must be checked here......(this comes from a reduction to LL1 grammar)

    TypeBinding referenceType = type.resolveType(scope, true /* check bounds*/);

    // will check for null after dimensions are checked
    constant = Constant.NotAConstant;
    if (referenceType == VoidBinding) {
      scope.problemReporter().cannotAllocateVoidArray(this);
      referenceType = null;
    }

    // check the validity of the dimension syntax (and test for all null dimensions)
    int explicitDimIndex = -1;
    loop:
    for (int i = dimensions.length; --i >= 0; ) {
      if (dimensions[i] != null) {
        if (explicitDimIndex < 0) explicitDimIndex = i;
      } else if (explicitDimIndex > 0) {
        // should not have an empty dimension before an non-empty one
        scope.problemReporter().incorrectLocationForNonEmptyDimension(this, explicitDimIndex);
        break loop;
      }
    }

    // explicitDimIndex < 0 says if all dimensions are nulled
    // when an initializer is given, no dimension must be specified
    if (initializer == null) {
      if (explicitDimIndex < 0) {
        scope.problemReporter().mustDefineDimensionsOrInitializer(this);
      }
      // allow new List<?>[5] - only check for generic array when no initializer, since also checked
      // inside initializer resolution
      if (referenceType != null && !referenceType.isReifiable()) {
        scope.problemReporter().illegalGenericArray(referenceType, this);
      }
    } else if (explicitDimIndex >= 0) {
      scope.problemReporter().cannotDefineDimensionsAndInitializer(this);
    }

    // dimensions resolution
    for (int i = 0; i <= explicitDimIndex; i++) {
      if (dimensions[i] != null) {
        TypeBinding dimensionType = dimensions[i].resolveTypeExpecting(scope, IntBinding);
        if (dimensionType != null) {
          dimensions[i].computeConversion(scope, IntBinding, dimensionType);
        }
      }
    }

    // building the array binding
    if (referenceType != null) {
      if (dimensions.length > 255) {
        scope.problemReporter().tooManyDimensions(this);
      }
      this.resolvedType = scope.createArrayType(referenceType, dimensions.length);

      // check the initializer
      if (initializer != null) {
        if ((initializer.resolveTypeExpecting(scope, this.resolvedType)) != null)
          initializer.binding = (ArrayBinding) this.resolvedType;
      }
    }
    return this.resolvedType;
  }