com.sun.tools.javac.tree.JCTree.JCConditional Java Examples

The following examples show how to use com.sun.tools.javac.tree.JCTree.JCConditional. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/** Visitor method for conditional expressions.
 */
@Override
public void visitConditional(JCConditional tree) {
    JCTree cond = tree.cond = translate(tree.cond, syms.booleanType);
    if (isTrue(cond)) {
        result = convert(translate(tree.truepart, tree.type), tree.type);
        addPrunedInfo(cond);
    } else if (isFalse(cond)) {
        result = convert(translate(tree.falsepart, tree.type), tree.type);
        addPrunedInfo(cond);
    } else {
        // Condition is not a compile-time constant.
        tree.truepart = translate(tree.truepart, tree.type);
        tree.falsepart = translate(tree.falsepart, tree.type);
        result = tree;
    }
}
 
Example #2
Source File: CRTable.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void visitConditional(JCConditional tree) {
    SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
    sr.mergeWith(csp(tree.cond));
    sr.mergeWith(csp(tree.truepart));
    sr.mergeWith(csp(tree.falsepart));
    result = sr;
}
 
Example #3
Source File: UConditional.java    From Refaster with Apache License 2.0 5 votes vote down vote up
@Override
public JCConditional inline(Inliner inliner) throws CouldNotResolveImportException {
  return inliner.maker().Conditional(
      getCondition().inline(inliner),
      getTrueExpression().inline(inliner), 
      getFalseExpression().inline(inliner));
}
 
Example #4
Source File: PrettyCommentsPrinter.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void visitConditional(JCConditional tree) {
	try {
		open(prec, TreeInfo.condPrec);
		printExpr(tree.cond, TreeInfo.condPrec);
		print(" ? ");
		printExpr(tree.truepart, TreeInfo.condPrec);
		print(" : ");
		printExpr(tree.falsepart, TreeInfo.condPrec);
		close(prec, TreeInfo.condPrec);
	} catch (IOException e) {
		throw new UncheckedIOException(e);
	}
}
 
Example #5
Source File: TransTypes.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void visitConditional(JCConditional tree) {
    tree.cond = translate(tree.cond, syms.booleanType);
    tree.truepart = translate(tree.truepart, erasure(tree.type));
    tree.falsepart = translate(tree.falsepart, erasure(tree.type));
    tree.type = erasure(tree.type);
    result = retype(tree, tree.type, pt);
}
 
Example #6
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void visitConditional(JCConditional tree) {
    scanCond(tree.cond);
    final Bits initsBeforeElse = new Bits(initsWhenFalse);
    final Bits uninitsBeforeElse = new Bits(uninitsWhenFalse);
    inits.assign(initsWhenTrue);
    uninits.assign(uninitsWhenTrue);
    if (tree.truepart.type.hasTag(BOOLEAN) &&
        tree.falsepart.type.hasTag(BOOLEAN)) {
        // if b and c are boolean valued, then
        // v is (un)assigned after a?b:c when true iff
        //    v is (un)assigned after b when true and
        //    v is (un)assigned after c when true
        scanCond(tree.truepart);
        final Bits initsAfterThenWhenTrue = new Bits(initsWhenTrue);
        final Bits initsAfterThenWhenFalse = new Bits(initsWhenFalse);
        final Bits uninitsAfterThenWhenTrue = new Bits(uninitsWhenTrue);
        final Bits uninitsAfterThenWhenFalse = new Bits(uninitsWhenFalse);
        inits.assign(initsBeforeElse);
        uninits.assign(uninitsBeforeElse);
        scanCond(tree.falsepart);
        initsWhenTrue.andSet(initsAfterThenWhenTrue);
        initsWhenFalse.andSet(initsAfterThenWhenFalse);
        uninitsWhenTrue.andSet(uninitsAfterThenWhenTrue);
        uninitsWhenFalse.andSet(uninitsAfterThenWhenFalse);
    } else {
        scanExpr(tree.truepart);
        final Bits initsAfterThen = new Bits(inits);
        final Bits uninitsAfterThen = new Bits(uninits);
        inits.assign(initsBeforeElse);
        uninits.assign(uninitsBeforeElse);
        scanExpr(tree.falsepart);
        inits.andSet(initsAfterThen);
        uninits.andSet(uninitsAfterThen);
    }
}
 
Example #7
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private ConditionalExpression convertConditional(JCConditional conditionalExpression) {
  return ConditionalExpression.newBuilder()
      .setTypeDescriptor(environment.createTypeDescriptor(conditionalExpression.type))
      .setConditionExpression(convertExpression(conditionalExpression.getCondition()))
      .setTrueExpression(convertExpression(conditionalExpression.getTrueExpression()))
      .setFalseExpression(convertExpression(conditionalExpression.getFalseExpression()))
      .build();
}
 
Example #8
Source File: ExpressionTemplate.java    From Refaster with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the precedence level appropriate for unambiguously printing
 * leaf as a subexpression of its parent.
 */
private static int getPrecedence(JCTree leaf, Context context) {
  JCCompilationUnit comp = context.get(JCCompilationUnit.class);
  JCTree parent = TreeInfo.pathFor(leaf, comp).get(1);

  // In general, this should match the logic in com.sun.tools.javac.tree.Pretty.
  //
  // TODO(mdempsky): There are probably cases where we could omit parentheses
  // by tweaking the returned precedence, but they need careful review.
  // For example, consider a template to replace "add(a, b)" with "a + b",
  // which applied to "x + add(y, z)" would result in "x + (y + z)".
  // In most cases, we'd likely prefer "x + y + z" instead, but those aren't
  // always equivalent: "0L + (Integer.MIN_VALUE + Integer.MIN_VALUE)" yields
  // a different value than "0L + Integer.MIN_VALUE + Integer.MIN_VALUE" due
  // to integer promotion rules.

  if (parent instanceof JCConditional) {
    // This intentionally differs from Pretty, because Pretty appears buggy:
    // http://mail.openjdk.java.net/pipermail/compiler-dev/2013-September/007303.html
    JCConditional conditional = (JCConditional) parent;
    return TreeInfo.condPrec + ((conditional.cond == leaf) ? 1 : 0);
  } else if (parent instanceof JCAssign) {
    JCAssign assign = (JCAssign) parent;
    return TreeInfo.assignPrec + ((assign.lhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCAssignOp) {
    JCAssignOp assignOp = (JCAssignOp) parent;
    return TreeInfo.assignopPrec + ((assignOp.lhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCUnary) {
    return TreeInfo.opPrec(parent.getTag());
  } else if (parent instanceof JCBinary) {
    JCBinary binary = (JCBinary) parent;
    return TreeInfo.opPrec(parent.getTag()) + ((binary.rhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCTypeCast) {
    JCTypeCast typeCast = (JCTypeCast) parent;
    return (typeCast.expr == leaf) ? TreeInfo.prefixPrec : TreeInfo.noPrec;
  } else if (parent instanceof JCInstanceOf) {
    JCInstanceOf instanceOf = (JCInstanceOf) parent;
    return TreeInfo.ordPrec + ((instanceOf.clazz == leaf) ? 1 : 0);
  } else if (parent instanceof JCArrayAccess) {
    JCArrayAccess arrayAccess = (JCArrayAccess) parent;
    return (arrayAccess.indexed == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec;
  } else if (parent instanceof JCFieldAccess) {
    JCFieldAccess fieldAccess = (JCFieldAccess) parent;
    return (fieldAccess.selected == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec;
  } else {
    return TreeInfo.noPrec;
  }
}
 
Example #9
Source File: HandleWither.java    From EasyMPermission with MIT License 4 votes vote down vote up
public JCMethodDecl createWither(long access, JavacNode field, JavacTreeMaker maker, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
	String witherName = toWitherName(field);
	if (witherName == null) return null;
	
	JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
	
	ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
	List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN);
	List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN);
	
	Name methodName = field.toName(witherName);
	List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables);
	
	long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, field.getContext());
	JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, annsOnParam), fieldDecl.name, fieldDecl.vartype, null);
	
	JCExpression selfType = cloneSelfType(field);
	if (selfType == null) return null;
	
	ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
	for (JavacNode child : field.up().down()) {
		if (child.getKind() != Kind.FIELD) continue;
		JCVariableDecl childDecl = (JCVariableDecl) child.get();
		// Skip fields that start with $
		if (childDecl.name.toString().startsWith("$")) continue;
		long fieldFlags = childDecl.mods.flags;
		// Skip static fields.
		if ((fieldFlags & Flags.STATIC) != 0) continue;
		// Skip initialized final fields.
		if (((fieldFlags & Flags.FINAL) != 0) && childDecl.init != null) continue;
		if (child.get() == field.get()) {
			args.append(maker.Ident(fieldDecl.name));
		} else {
			args.append(createFieldAccessor(maker, child, FieldAccess.ALWAYS_FIELD));
		}
	}
	
	JCNewClass newClass = maker.NewClass(null, List.<JCExpression>nil(), selfType, args.toList(), null);
	JCExpression identityCheck = maker.Binary(CTC_EQUAL, createFieldAccessor(maker, field, FieldAccess.ALWAYS_FIELD), maker.Ident(fieldDecl.name));
	JCConditional conditional = maker.Conditional(identityCheck, maker.Ident(field.toName("this")), newClass);
	JCReturn returnStatement = maker.Return(conditional);
	
	if (nonNulls.isEmpty()) {
		statements.append(returnStatement);
	} else {
		JCStatement nullCheck = generateNullCheck(maker, field, source);
		if (nullCheck != null) statements.append(nullCheck);
		statements.append(returnStatement);
	}
	
	JCExpression returnType = cloneSelfType(field);
	
	JCBlock methodBody = maker.Block(0, statements.toList());
	List<JCTypeParameter> methodGenericParams = List.nil();
	List<JCVariableDecl> parameters = List.of(param);
	List<JCExpression> throwsClauses = List.nil();
	JCExpression annotationMethodDefaultValue = null;
	
	List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod);
	
	if (isFieldDeprecated(field)) {
		annsOnMethod = annsOnMethod.prepend(maker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil()));
	}
	JCMethodDecl decl = recursiveSetGeneratedBy(maker.MethodDef(maker.Modifiers(access, annsOnMethod), methodName, returnType,
			methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source.get(), field.getContext());
	copyJavadoc(field, decl, CopyJavadoc.WITHER);
	return decl;
}
 
Example #10
Source File: JavacTreeMaker.java    From EasyMPermission with MIT License 4 votes vote down vote up
public JCConditional Conditional(JCExpression cond, JCExpression thenpart, JCExpression elsepart) {
	return invoke(Conditional, cond, thenpart, elsepart);
}
 
Example #11
Source File: ArgumentAttr.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
ArgumentType<JCConditional> dup(JCConditional tree, Env<AttrContext> env) {
    return new ConditionalType(tree, env, speculativeTree, speculativeTypes);
}
 
Example #12
Source File: ArgumentAttr.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
ConditionalType(JCExpression tree, Env<AttrContext> env, JCConditional speculativeCond, Map<ResultInfo, Type> speculativeTypes) {
   super(tree, env, speculativeCond, speculativeTypes);
}
 
Example #13
Source File: ArgumentAttr.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
ConditionalType(JCExpression tree, Env<AttrContext> env, JCConditional speculativeCond) {
    this(tree, env, speculativeCond, new HashMap<>());
}
 
Example #14
Source File: ArgumentAttr.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visitConditional(JCConditional that) {
    processArg(that, speculativeTree -> new ConditionalType(that, env, speculativeTree));
}
 
Example #15
Source File: MemberEnter.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void visitConditional(JCConditional tree) {
    tree.cond.accept(this);
    tree.truepart.accept(this);
    tree.falsepart.accept(this);
}
 
Example #16
Source File: ArgumentAttr.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
ArgumentType<JCConditional> dup(JCConditional tree, Env<AttrContext> env) {
    return new ConditionalType(tree, env, speculativeTree, speculativeTypes);
}
 
Example #17
Source File: ArgumentAttr.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
ConditionalType(JCExpression tree, Env<AttrContext> env, JCConditional speculativeCond, Map<ResultInfo, Type> speculativeTypes) {
   super(tree, env, speculativeCond, speculativeTypes);
}
 
Example #18
Source File: ArgumentAttr.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
ConditionalType(JCExpression tree, Env<AttrContext> env, JCConditional speculativeCond) {
    this(tree, env, speculativeCond, new HashMap<>());
}
 
Example #19
Source File: ArgumentAttr.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void visitConditional(JCConditional that) {
    processArg(that, speculativeTree -> new ConditionalType(that, env, speculativeTree));
}