Package org.openquark.cal.internal.javamodel.JavaExpression

Examples of org.openquark.cal.internal.javamodel.JavaExpression.LocalVariable


   
    private static boolean encodeLocalVariableDeclaration(JavaStatement.LocalVariableDeclaration declaration, GenerationContext context) throws JavaGenerationException{
       
        MethodVisitor mv = context.getMethodVisitor();
       
        LocalVariable localVariable = declaration.getLocalVariable();
       
        // encode the instructions for the initializer if any.      
        JavaExpression initializer = declaration.getInitializer();
        if (initializer != null) {
            encodeExpr(initializer, context);           
        }
       
        int localVarIndex = context.addLocalVar(localVariable.getName(), localVariable.getTypeName());
              
        // Store the value from the initializer (if any) into the local var.
        if (initializer != null) {
          
            //encode the store instruction
            mv.visitVarInsn(getStoreOpCode(localVariable.getTypeName()), localVarIndex);           
        }
             
        return false;
    }
View Full Code Here


       
        // Determine what type of creature this name represents.
        //   Note that because of the order of lookups, this takes scoping into account.
        String varName = localName.getName();
        if (context.getLocalVarIndex(varName) != -1) {
            return encodeLocalVariableExpr(new LocalVariable(varName, localName.getTypeName()), context);

        } else if (context.getMethodVarIndex(varName) != -1) {
            return encodeMethodVariableExpr(new MethodVariable(varName), context);

        } else {
View Full Code Here

            // Determine what type of creature this name represents.
            //   Note that because of the order of lookups, this takes scoping into account.
            LocalName localName = (LocalName)lhs;
            String varName = localName.getName();
            if (context.getLocalVarIndex(varName) != -1) {
                lhs = new LocalVariable(varName, localName.getTypeName());
            } else if (context.getMethodVarIndex(varName) != -1) {
                lhs = new MethodVariable(varName);
            } else {
                lhs = new JavaField.Instance(null, varName, localName.getTypeName());
            }
        }
       
        // assign the value
        if (lhs instanceof JavaField.Instance) {                      
                       
            JavaField.Instance javaInstanceField = (JavaField.Instance)lhs;           
                 
            //push the reference to the Java field itself. We can't just call encodeJavaField because we need the type of the instance object.
            JavaExpression instance = javaInstanceField.getInstance();
            JavaTypeName instanceType;
            if (instance == null) {
                //use 'this' as the instance expression, so if the field is 'foo', then it is 'this.foo'.
                instanceType = encodeThis(context);              
               
            } else {
                instanceType = encodeExpr(instance, context);              
            }
                      
            //push the rhs expression
            // the type of the assignment takes on the type of the value (not the assigned variable).      
            JavaTypeName returnType = encodeExpr(assignment.getValue(), context);
           
            if (retainValue) {
                //duplicate the value so that a copy will be left over after assignment
                //field, value --->
                //value, field, value
                mv.visitInsn(getDupX1OpCode(returnType));
            }
           
            //do the assignment
            mv.visitFieldInsn(Opcodes.PUTFIELD, instanceType.getJVMInternalName(), javaInstanceField.getFieldName(), javaInstanceField.getFieldType().getJVMDescriptor());
           
            return returnType;
           
        } else if (lhs instanceof JavaField.Static) {
           
            JavaField.Static javaStaticField = (JavaField.Static)lhs;
                                  
            //push the rhs expression
            // the type of the assignment takes on the type of the value (not the assigned variable).      
            JavaTypeName returnType = encodeExpr(assignment.getValue(), context);
           
            if (retainValue) {
                //duplicate the value so that a copy will be left over after assignment
                //value --->
                //value, value
                mv.visitInsn(getDupOpCode(returnType));
            }
           
            //do the assignment
            mv.visitFieldInsn(Opcodes.PUTSTATIC, javaStaticField.getInvocationClass().getJVMInternalName(), javaStaticField.getFieldName(), javaStaticField.getFieldType().getJVMDescriptor());           
           
            return returnType;
                  
        } else if (lhs instanceof LocalVariable) {
            LocalVariable localVariable = (LocalVariable)lhs;
                       
            int localVarIndex = context.getLocalVarIndex(localVariable.getName());
   
            //push the rhs expression
            JavaTypeName returnType = encodeExpr(assignment.getValue(), context);
           
            if (retainValue) {
                //duplicate the value so that a copy will be left over after assignment
                //value --->
                //value, value
                mv.visitInsn(getDupOpCode(returnType));
            }
                           
            //encode the store instruction
            mv.visitVarInsn(getStoreOpCode(localVariable.getTypeName()), localVarIndex);
           
            return returnType;
   
        } else if (lhs instanceof MethodVariable) {
            MethodVariable methodVariable = (MethodVariable)lhs;
View Full Code Here

    /* (non-Javadoc)
     * @see org.openquark.cal.internal.runtime.lecc.JavaModelVisitor#visitLocalVariableExpression(org.openquark.cal.internal.runtime.lecc.JavaExpression.LocalVariable, java.lang.Object)
     */
    public JavaExpression visitLocalVariableExpression(LocalVariable localVariable,
            T arg) {
        return new LocalVariable(localVariable.getName(), localVariable.getTypeName());
    }
View Full Code Here

         */
        @Override
        public Void visitLocalVariableDeclarationStatement(
                LocalVariableDeclaration localVariableDeclaration, Void arg) {

            LocalVariable localVariable = localVariableDeclaration.getLocalVariable();
            context.getSourceName(localVariable.getTypeName());

            return super.visitLocalVariableDeclarationStatement(localVariableDeclaration, arg);
        }
View Full Code Here

    private static StringBuilder getSource(JavaStatement statement, GenerationContext context, int indent) throws JavaGenerationException {
        StringBuilder sb = new StringBuilder();

        if (statement instanceof LocalVariableDeclaration) {
            LocalVariableDeclaration declaration = (LocalVariableDeclaration)statement;
            LocalVariable localVariable = declaration.getLocalVariable();

            emitIndent(sb, indent);

            // final
            if (declaration.isFinal()) {
                sb.append("final ");
            }

            // variable type and name
            sb.append(context.getSourceName(localVariable.getTypeName()) + " " + localVariable.getName());

            // " = " + initializer
            JavaExpression initializer = declaration.getInitializer();
            if (initializer != null) {
                sb.append(" = ");
View Full Code Here

           
            hashCode.addStatement (ifThen);
           
            // build up the hashcode value.
            JavaExpression.LocalVariable result =
                new LocalVariable ("result", JavaTypeName.INT);
            LocalVariableDeclaration localVarDecl =
                new LocalVariableDeclaration(result, LiteralWrapper.make(new Integer(17)));
            thenBlock.addStatement(localVarDecl);
           
            LiteralWrapper thirtySeven = LiteralWrapper.make (new Integer(37));
            JavaExpression thirtySevenTimesResult =
                new OperatorExpression.Binary(JavaOperator.MULTIPLY_INT, thirtySeven, result);

            LiteralWrapper zero = LiteralWrapper.make (new Integer (0));
            LiteralWrapper one  = LiteralWrapper.make (new Integer (1));

            // Start by including dc name in the hashcode.
            // get objects hashcode
            JavaExpression nameExpression =
                new MethodInvocation.Instance (
                        new MethodInvocation.Instance(
                                null,
                                GET_DC_NAME_METHOD_NAME,
                                JavaTypeName.STRING,
                                MethodInvocation.InvocationType.VIRTUAL),
                        "hashCode",
                        JavaTypeName.INT,
                        MethodInvocation.InvocationType.VIRTUAL);

            JavaExpression newResult =
                new OperatorExpression.Binary (JavaOperator.ADD_INT, thirtySevenTimesResult, nameExpression);
            JavaExpression assignResult = new Assignment (result, newResult);
            thenBlock.addStatement(new ExpressionStatement (assignResult));
           
            // Now get the contribution from each dc field.
            for (FieldName fn : fieldNames) {
                String javaFieldName = typeConstructorInfo.fieldJavaNames.get (fn);
                JavaTypeName fieldType = fieldNameToType.get (fn);
                JavaField field = new JavaField.Instance(null, javaFieldName, fieldType);
               
                JavaExpression fieldExpression;
                if (fieldType instanceof JavaTypeName.Primitive) {
                    if (fieldType instanceof JavaTypeName.Primitive.Boolean) {
                        fieldExpression =
                            new OperatorExpression.Ternary (field, zero, one);
                    } else if (fieldType instanceof JavaTypeName.Primitive.Byte ||
                            fieldType instanceof JavaTypeName.Primitive.Char ||
                            fieldType instanceof JavaTypeName.Primitive.Short) {
                        fieldExpression =
                            new CastExpression(JavaTypeName.INT, field);
                    }else if (fieldType instanceof JavaTypeName.Primitive.Double) {
                        // long f = Double.doubleToLongBits(f);
                        // result = (int) (f ^ (f >>> 32));
                        JavaExpression.LocalVariable f =
                            new LocalVariable ("f", JavaTypeName.LONG);
                        JavaExpression initializeF =
                            new MethodInvocation.Static (
                                    JavaTypeName.DOUBLE_OBJECT,
                                    "doubleToLongBits",
                                    field,
View Full Code Here

                        condition,
                        new ReturnStatement(LiteralWrapper.FALSE));
            equals.addStatement(ifThen);
           
            // ThisType castObject = (ThisType)object;
            LocalVariable localVar =
                new LocalVariable ("cast" + argName, typeName);
           
            JavaStatement localVarDecl =
                new JavaStatement.LocalVariableDeclaration (
                        localVar,
                        new JavaExpression.CastExpression(typeName, objectArg));
View Full Code Here

        public JavaStatement visitLocalVariableDeclarationStatement(
                LocalVariableDeclaration localVariableDeclaration, Void arg) {

            // The 'lastRef' transformation should not be applied to the left hand side of
            // the declaration.
            LocalVariable assignTo = (LocalVariable)localVariableDeclaration.getLocalVariable().accept(new JavaModelCopier<Void>(), null);

            if (localVariableDeclaration.getInitializer() != null) {
                LocalVariableDeclaration newDeclaration =
                    new LocalVariableDeclaration (
                        assignTo,
View Full Code Here

                }
            }

            // Add a default case in the switch to throw an error if an invalid ordinal value is used.
            Block defaultBlock = new Block();
            LocalVariable bf = new LocalVariable("bf", JavaTypeName.STRING_BUILDER);
            defaultBlock.addStatement(new LocalVariableDeclaration (bf, new ClassInstanceCreationExpression(JavaTypeName.STRING_BUILDER)));
            LiteralWrapper badValueMessageWrapper1 = LiteralWrapper.make("Invalid ordinal value of ");
            JavaExpression message = new MethodInvocation.Instance(bf, "append", badValueMessageWrapper1, JavaTypeName.STRING, JavaTypeName.STRING_BUILDER, MethodInvocation.InvocationType.VIRTUAL);
            message = new  MethodInvocation.Instance(message, "append", METHODVAR_ORDINAL, JavaTypeName.INT, JavaTypeName.STRING_BUILDER, MethodInvocation.InvocationType.VIRTUAL);
            LiteralWrapper badValueMessageWrapper2 = LiteralWrapper.make(" in " + className.toString() + ".getTagDC().");
View Full Code Here

TOP

Related Classes of org.openquark.cal.internal.javamodel.JavaExpression.LocalVariable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.