Java Code Examples for com.sun.tools.javac.tree.JCTree#JCBinary

The following examples show how to use com.sun.tools.javac.tree.JCTree#JCBinary . 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: StringConcat.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Item makeConcat(JCTree.JCBinary tree) {
    JCDiagnostic.DiagnosticPosition pos = tree.pos();

    // Create a string builder.
    newStringBuilder(tree);

    // Append all strings to builder.
    List<JCTree> args = collectAll(tree);
    for (JCTree t : args) {
        gen.genExpr(t, t.type).load();
        appendString(t);
    }

    // Convert builder to string.
    builderToString(pos);

    return gen.getItems().makeStackItem(syms.stringType);
}
 
Example 2
Source File: StringConcat.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Item makeConcat(JCTree.JCBinary tree) {
    JCDiagnostic.DiagnosticPosition pos = tree.pos();

    // Create a string builder.
    newStringBuilder(tree);

    // Append all strings to builder.
    List<JCTree> args = collectAll(tree);
    for (JCTree t : args) {
        gen.genExpr(t, t.type).load();
        appendString(t);
    }

    // Convert builder to string.
    builderToString(pos);

    return gen.getItems().makeStackItem(syms.stringType);
}
 
Example 3
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 6 votes vote down vote up
private JCTree.JCMethodInvocation configMethod( JCTree.JCBinary tree, Symbol.MethodSymbol operatorMethod, JCTree.JCMethodInvocation methodCall )
{
  methodCall.setPos( tree.pos );
  methodCall.type = operatorMethod.getReturnType();

  // If methodCall is an extension method, rewrite it
  Symbol.MethodSymbol extMethod = findExtMethod( methodCall );
  if( extMethod != null )
  {
    // Replace with extension method call
    methodCall = replaceExtCall( methodCall, extMethod );
  }

  // Concrete type set in attr
  methodCall.type = tree.type;
  return methodCall;
}
 
Example 4
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 6 votes vote down vote up
private JCTree[] tempify( JCTree.JCBinary tree, TreeMaker make, JCExpression expr, Context ctx, Symbol owner, String varName )
{
  switch( expr.getTag() )
  {
    case LITERAL:
    case IDENT:
      return null;

    default:
      JCTree.JCVariableDecl tempVar = make.VarDef( make.Modifiers( FINAL | SYNTHETIC ),
        Names.instance( ctx ).fromString( varName + tempVarIndex ), make.Type( expr.type ), expr );
      tempVar.sym = new Symbol.VarSymbol( FINAL | SYNTHETIC, tempVar.name, expr.type, owner );
      tempVar.type = tempVar.sym.type;
      tempVar.pos = tree.pos;
      JCExpression ident = make.Ident( tempVar );
      ident.type = expr.type;
      ident.pos = tree.pos;
      return new JCTree[] {tempVar, ident};
  }
}
 
Example 5
Source File: StringConcat.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<JCTree> collect(JCTree tree, List<JCTree> res) {
    tree = TreeInfo.skipParens(tree);
    if (tree.hasTag(PLUS) && tree.type.constValue() == null) {
        JCTree.JCBinary op = (JCTree.JCBinary) tree;
        if (op.operator.kind == MTH && op.operator.opcode == string_add) {
            return res
                    .appendList(collect(op.lhs, res))
                    .appendList(collect(op.rhs, res));
        }
    }
    return res.append(tree);
}
 
Example 6
Source File: ManAttr.java    From manifold with Apache License 2.0 5 votes vote down vote up
default void visitBindingExpression( JCTree.JCBinary tree )
{
  Type owntype;

  if( IDynamicJdk.instance().getOperator( tree ) == null )
  {
    // replace the tree with JCBinary expressions reflecting the correct associativity and bindingOperator
    JCTree.JCBinary newTree = new JavacBinder( types() ).bind( getBindingOperands( tree, new ArrayList<>() ) );

    if( newTree == null )
    {
      getLogger().error( tree.lhs.pos,
        "proc.messager", "No reaction defined for types '" + tree.lhs.type + "' and '" + tree.rhs.type + "'" );
      return;
    }

    ReflectUtil.field( tree, "opcode" ).set( ReflectUtil.field( newTree, "opcode" ).get() );
    tree.lhs = newTree.lhs;
    tree.rhs = newTree.rhs;
    tree.type = newTree.type;
    IDynamicJdk.instance().setOperator( tree, (Symbol.OperatorSymbol)IDynamicJdk.instance().getOperator( newTree ) );
    owntype = newTree.type;
  }
  else
  {
    Symbol operator = IDynamicJdk.instance().getOperator( tree );
    owntype = operator.type.isErroneous() ? operator.type : operator.type.getReturnType();
  }

  setResult( tree, owntype );
}
 
Example 7
Source File: ManAttr_8.java    From manifold with Apache License 2.0 5 votes vote down vote up
@Override
public void visitBinary( JCTree.JCBinary tree )
{
  if( !JavacPlugin.instance().isExtensionsEnabled() )
  {
    super.visitBinary( tree );
    return;
  }

  if( tree.getTag() == JCTree.Tag.APPLY ) // binding expr
  {
    // Handle binding expressions

    visitBindingExpression( tree );
    ReflectUtil.field( tree, "opcode" ).set( JCTree.Tag.MUL ); // pose as a MUL expr to pass binary expr checks
    return;
  }

  ReflectUtil.LiveMethodRef checkNonVoid = ReflectUtil.method( chk(), "checkNonVoid", DiagnosticPosition.class, Type.class );
  ReflectUtil.LiveMethodRef attribExpr = ReflectUtil.method( this, "attribExpr", JCTree.class, Env.class );
  Type left = (Type)checkNonVoid.invoke( tree.lhs.pos(), attribExpr.invoke( tree.lhs, getEnv() ) );
  Type right = (Type)checkNonVoid.invoke( tree.lhs.pos(), attribExpr.invoke( tree.rhs, getEnv() ) );

  if( handleOperatorOverloading( tree, left, right ) )
  {
    // Handle operator overloading
    return;
  }

  // Everything after left/right operand attribution (see super.visitBinary())
  _visitBinary_Rest( tree, left, right );
}
 
Example 8
Source File: JavaDynamicJdk_8.java    From manifold with Apache License 2.0 5 votes vote down vote up
@Override
public void setOperator( JCTree.JCExpression tree, Symbol.OperatorSymbol operator )
{
  if( tree instanceof JCTree.JCBinary )
  {
    ((JCTree.JCBinary)tree).operator = operator;
  }
  else
  {
    ((JCTree.JCUnary)tree).operator = operator;
  }
}
 
Example 9
Source File: JavaDynamicJdk_8.java    From manifold with Apache License 2.0 5 votes vote down vote up
public void setOperatorSymbol( Context ctx, JCTree.JCBinary cond, JCTree.Tag tag, String op, Symbol operandType )
{
  Symbol.OperatorSymbol operatorSym = (Symbol.OperatorSymbol)IDynamicJdk.instance().getMembers(
    Symtab.instance( ctx ).predefClass,
    (Symbol s) -> s instanceof Symbol.OperatorSymbol &&
                  s.name.toString().equals( op ) &&
                  ((Symbol.MethodSymbol)s).params().get( 0 ).type.tsym == operandType )
    .iterator().next(); // should be just one
  setOperator( cond, operatorSym );
}
 
Example 10
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 5 votes vote down vote up
/**
 * If the binding expression is of the form `A b` where `A` is a Float or Double *literal* and `b` defines a
 * `R postfixBind(String)` where `R` is the same return type as the original binding expression, then get the
 * token that was parsed for the Float or Double literal and use the String version of `postfixBind()`. This has
 * the effect of preserving the value of the token, where otherwise it can be lost due to IEEE floating point
 * encoding.
 */
private Symbol.MethodSymbol favorStringsWithNumberCoercion( JCTree.JCBinary tree, Symbol.MethodSymbol operatorMethod )
{
  String operatorMethodName = operatorMethod.name.toString();
  if( !operatorMethodName.equals( "postfixBind" ) )
  {
    return operatorMethod;
  }

  if( tree.lhs instanceof JCTree.JCLiteral &&
      (tree.lhs.getKind() == Tree.Kind.FLOAT_LITERAL || tree.lhs.getKind() == Tree.Kind.DOUBLE_LITERAL) )
  {
    Type rhsType = tree.rhs.type;
    Symbol.MethodSymbol postfixBinding_string = ManAttr.getMethodSymbol(
      _tp.getTypes(), rhsType, _tp.getSymtab().stringType, operatorMethodName, (Symbol.ClassSymbol)rhsType.tsym, 1 );

    if( postfixBinding_string != null &&
        postfixBinding_string.getParameters().get( 0 ).type.tsym == _tp.getSymtab().stringType.tsym &&
        postfixBinding_string.getReturnType().equals( operatorMethod.getReturnType() ) )
    {
      // since the source may be preprocessed we attempt to get it in its preprocessed form
      CharSequence source = ManParserFactory.getSource( _tp.getCompilationUnit().getSourceFile() );
      int start = tree.lhs.pos;
      int end = tree.lhs.pos().getEndPosition( ((JCTree.JCCompilationUnit)_tp.getCompilationUnit()).endPositions );
      String token = source.subSequence( start, end ).toString();
      if( token.endsWith( "d" ) || token.endsWith( "f" ) )
      {
        token = token.substring( 0, token.length()-1 );
      }
      JCTree.JCLiteral temp = (JCTree.JCLiteral)tree.lhs;
      tree.lhs = _tp.getTreeMaker().Literal( token );
      tree.lhs.type = _tp.getSymtab().stringType;
      tree.lhs.pos = temp.pos;

      return postfixBinding_string;
    }
  }
  return operatorMethod;
}
 
Example 11
Source File: StringLiteralTemplateProcessor.java    From manifold with Apache License 2.0 5 votes vote down vote up
@Override
public void visitLiteral( JCTree.JCLiteral jcLiteral )
{
  super.visitLiteral( jcLiteral );

  Object value = jcLiteral.getValue();
  if( !(value instanceof String) )
  {
    return;
  }

  if( isDisabled() )
  {
    return;
  }

  TreeMaker maker = TreeMaker.instance( _javacTask.getContext() );
  String stringValue = (String)value;
  List<JCTree.JCExpression> exprs = parse( stringValue, jcLiteral.getPreferredPosition() );
  JCTree.JCBinary concat = null;
  while( !exprs.isEmpty() )
  {
    if( concat == null )
    {
      concat = maker.Binary( JCTree.Tag.PLUS, exprs.remove( 0 ), exprs.remove( 0 ) );
    }
    else
    {
      concat = maker.Binary( JCTree.Tag.PLUS, concat, exprs.remove( 0 ) );
    }
  }

  result = concat == null ? result : concat;
}
 
Example 12
Source File: StringConcat.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private List<JCTree> collect(JCTree tree, List<JCTree> res) {
    tree = TreeInfo.skipParens(tree);
    if (tree.hasTag(PLUS) && tree.type.constValue() == null) {
        JCTree.JCBinary op = (JCTree.JCBinary) tree;
        if (op.operator.kind == MTH && op.operator.opcode == string_add) {
            return res
                    .appendList(collect(op.lhs, res))
                    .appendList(collect(op.rhs, res));
        }
    }
    return res.append(tree);
}
 
Example 13
Source File: StringConcat.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Item makeConcat(JCTree.JCBinary tree) {
    List<JCTree> args = collectAll(tree.lhs, tree.rhs);
    emit(args, tree.type, tree.pos());
    return gen.getItems().makeStackItem(syms.stringType);
}
 
Example 14
Source File: JavaDynamicJdk_8.java    From manifold with Apache License 2.0 4 votes vote down vote up
@Override
public Symbol getOperator( JCTree.JCExpression tree )
{
  return tree instanceof JCTree.JCBinary ? ((JCTree.JCBinary)tree).operator : ((JCTree.JCUnary)tree).operator;
}
 
Example 15
Source File: StringConcat.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Item makeConcat(JCTree.JCBinary tree) {
    List<JCTree> args = collectAll(tree.lhs, tree.rhs);
    emit(args, tree.type, tree.pos());
    return gen.getItems().makeStackItem(syms.stringType);
}
 
Example 16
Source File: SampleJavacPlugin.java    From tutorials with MIT License 4 votes vote down vote up
private static JCTree.JCBinary createIfCondition(TreeMaker factory, Names symbolsTable, VariableTree parameter) {
    Name parameterId = symbolsTable.fromString(parameter.getName().toString());
    return factory.Binary(JCTree.Tag.LE, 
      factory.Ident(parameterId), 
      factory.Literal(TypeTag.INT, 0));
}
 
Example 17
Source File: StringConcat.java    From openjdk-jdk9 with GNU General Public License v2.0 votes vote down vote up
public abstract Item makeConcat(JCTree.JCBinary tree); 
Example 18
Source File: IDynamicJdk.java    From manifold with Apache License 2.0 votes vote down vote up
void setOperatorSymbol( Context ctx, JCTree.JCBinary expr, JCTree.Tag tag, String op, Symbol operandType ); 
Example 19
Source File: StringConcat.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License votes vote down vote up
public abstract Item makeConcat(JCTree.JCBinary tree);