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

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


            JavaTypeName onFailureExprType = assertStatement.getOnFailureExprType();
            if (onFailureExprType instanceof JavaTypeName.Reference) {
                onFailureExprType = JavaTypeName.OBJECT;
            }
            createAssertionError =
                new ClassInstanceCreationExpression (JavaTypeName.ASSERTION_ERROR,
                                                     assertStatement.getOnFailureExpr(),
                                                     onFailureExprType);
        } else {
            createAssertionError =
                new ClassInstanceCreationExpression(JavaTypeName.ASSERTION_ERROR);
        }
        JavaStatement.ThrowStatement throwAssertError =
            new ThrowStatement(createAssertionError);
       
        // if (!conditionExpr) {throw new AssertionError();}
View Full Code Here


        for (int i = 0, n = argTypes.length; i < n; ++i) {
            argTypes[i] = instanceCreation.getParamType(i);
            argValues[i] = (JavaExpression)instanceCreation.getArg(i).accept(this, arg);
        }
       
        return new ClassInstanceCreationExpression(instanceCreation.getClassName(), argValues, argTypes);
    }
View Full Code Here

            for (int i = argTypes.length - 1; i >= 0; --i) {
                argTypes[i] = instanceCreation.getParamType(i);
                argValues[i] = (JavaExpression)instanceCreation.getArg(i).accept(this, arg);
            }

            return new ClassInstanceCreationExpression(instanceCreation.getClassName(), argValues, argTypes);
        }
View Full Code Here

        private void createFields_tagDCs () {
            JavaTypeName tagDCTypeName = CALToJavaNames.createTypeNameForTagDCFromType(typeConstructor, module);
            int fieldModifiers = Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL;
            for (final DataConstructor dc : dataConsList) {
                if (dc.getArity() == 0) {
                    JavaExpression initializer = new ClassInstanceCreationExpression(tagDCTypeName, LiteralWrapper.make(Integer.valueOf(dc.getOrdinal())), JavaTypeName.INT);
                    String fieldName = CALToJavaNames.fixupVarName(dc.getName().getUnqualifiedName());
                    JavaFieldDeclaration fieldDec = new JavaFieldDeclaration(fieldModifiers, tagDCTypeName, fieldName, initializer);
                    javaClassRep.addFieldDeclaration(fieldDec);
                }
            }
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().");
            message = new MethodInvocation.Instance(message, "append", badValueMessageWrapper2, JavaTypeName.STRING, JavaTypeName.STRING_BUILDER, MethodInvocation.InvocationType.VIRTUAL);
View Full Code Here

                final int functionIndex = functions.getFunctionIndex(mf.getName());
                final JavaExpression initializer;

                if (functions.getNFunctions() <= 1) {
                    initializer = new ClassInstanceCreationExpression(
                            className);
                } else {
                    initializer = new ClassInstanceCreationExpression(
                            className,
                            new JavaExpression[]{LiteralWrapper.make(Integer.valueOf(functionIndex)),
                                                 LiteralWrapper.make(Integer.valueOf(mf.getArity()))},
                            new JavaTypeName[]{JavaTypeName.INT,
                                               JavaTypeName.INT});
                }

                final String fieldName = CALToJavaNames.getInstanceFieldName(mf.getQualifiedName(), module);
                final int instanceModifiers;
                if (mf.getArity() == 0) {
                    //for a CAF or a zero-arity function, the instance field will never be referred to directly outside the class
                    //so declare them private for safety sake.
                    instanceModifiers = Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL;
                } else {
                    instanceModifiers = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
                }
                final JavaFieldDeclaration instanceDeclaration =
                    new JavaFieldDeclaration(instanceModifiers,
                            className,
                            fieldName,
                            initializer);

                if (functions.getNFunctions() <= 1) {
                    instanceDeclaration.setJavaDoc(new JavaDocComment("Singleton instance of this class."));
                } else {
                    instanceDeclaration.setJavaDoc(new JavaDocComment("Instance of this class representing CAL function " + mf.getName() + "."));
                }
                javaClassRep.addFieldDeclaration(instanceDeclaration);
            }


            if (sharedValues.getNStaticErrorInfo() > 0) {
                javaClassRep.addComment(new JavaStatement.MultiLineComment("ErrorInfo instances."));
            }
            for (final String name : sharedValues.getStaticErrorInfoNames()) {

                final JavaExpression initializer = sharedValues.getStaticError(name);
                final int instanceModifiers = Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL;
                final JavaFieldDeclaration errorDeclaration = new JavaFieldDeclaration(instanceModifiers, JavaTypeName.ERRORINFO, name, initializer);
                javaClassRep.addFieldDeclaration(errorDeclaration);

            }

            // If this function references other supercombinators we need to set
            // up a field for each referenced SC, a flag to indicate the
            // initialization state of the referenced SC fields, and potentially an
            // object to be used as a synchronization mutex for the initialization method.
            final int referencedDCModifiers = Modifier.STATIC | Modifier.PRIVATE | Modifier.FINAL;

            // Create an instance field for each referenced data constructor.  This will give us
            // a local reference that can be used in the body function.
            //  e.g. RTFunction i_Foo;
            if (sharedValues.getNReferencedDCs() > 0) {
                javaClassRep.addComment(new JavaStatement.MultiLineComment("Data constructor class instances for all referenced data constructors."));
            }
            for (final ReferencedDCInfo rfi : sharedValues.getReferencedDCs()) {
                DataConstructor dc = rfi.getDC();
                JavaField jf = rfi.getJField();
                JavaExpression fieldInitializer;
                if (dc.getArity() > 0) {
                    // Just invoke the regular make invocation to get the singleton SC/DC instance.
                    fieldInitializer =
                        new MethodInvocation.Static(jf.getFieldType(), "make", JavaExpression.EMPTY_JAVA_EXPRESSION_ARRAY, JavaExpression.EMPTY_TYPE_NAME_ARRAY, jf.getFieldType());
                } else {
                    // This is a zero arity data constructor.  We get the singleton instance by accessing
                    // the DataType class factory method if the data type has more than one zero arity DC.
                    TypeConstructor typeCons = dc.getTypeConstructor();
                    if (SCJavaDefn.isTagDC(dc, module)) {
                        JavaTypeName typeClass = CALToJavaNames.createTypeNameFromType(typeCons, module);
                        JavaTypeName tagDCTypeName = CALToJavaNames.createTypeNameForTagDCFromType(typeCons, module);
                        Integer ordinal = Integer.valueOf(dc.getOrdinal());
                        fieldInitializer = new MethodInvocation.Static(typeClass, "getTagDC", LiteralWrapper.make(ordinal), JavaTypeName.INT, tagDCTypeName);
                    } else {
                        // Just invoke the regular make invocation to get the singleton SC/DC instance.
                        fieldInitializer =
                            new MethodInvocation.Static(jf.getFieldType(), "make", JavaExpression.EMPTY_JAVA_EXPRESSION_ARRAY, JavaExpression.EMPTY_TYPE_NAME_ARRAY, jf.getFieldType());
                    }
                }

                JavaFieldDeclaration dcDeclaration =
                    new JavaFieldDeclaration(
                            referencedDCModifiers,
                            jf.getFieldType(),
                            jf.getFieldName(),
                            fieldInitializer);
                javaClassRep.addFieldDeclaration(dcDeclaration);
            }

            if (functions.includesCAFs()) {

                javaClassRep.addComment(new JavaStatement.MultiLineComment("Mappings of execution context to CAF instances."));
                // Add an instance field to hold a Map of
                // ExecutionContext -> RTFullApp.General._0
                // A map for associating instances of this SC with execution contexts.
                // This is only created for CAF functions.  There are two reasons why CAFs
                // have an instance for each execution context.  One is thread safety.  The
                // other is so that different threads of execution can release cached CAF
                // results at will.
                for (final MachineFunction mf : functions.getTopLevelCALFunctions()) {
                    if (mf.isCAF()) {

                        //we do not synchronize the instance map, but rather the methods in the CAF class that mutate it.
                        //this is because the make method has check-then-modify semantics, and so needs to be synchronized at the method
                        //level anyways.
                        JavaExpression instancesMapInit = new ClassInstanceCreationExpression(JavaTypeName.WEAK_HASH_MAP);

                        String mapPrefix = functions.getFNamePrefix(mf.getName());
                        JavaFieldDeclaration instancesMap =
                            new JavaFieldDeclaration (
                                Modifier.STATIC | Modifier.PRIVATE | Modifier.FINAL,
View Full Code Here

                        // This is an unsafe method.  i.e. a non-CAF function of arity zero.  Since the
                        // function can have side effects we need to create a new instance of the class
                        // each time so that a previously evaluated value doesn't get re-used in place
                        // of actually executing the function.
                        JavaExpression.LocalVariable newInstanceVar = new JavaExpression.LocalVariable("newInstance", returnType);
                        JavaExpression initializer = new ClassInstanceCreationExpression(_0TypeName, instanceField, JavaTypeNames.RTSUPERCOMBINATOR);
                        JavaStatement decl = new JavaStatement.LocalVariableDeclaration(newInstanceVar, initializer);
                        b.addStatement(decl);
                        b.addStatement(new ReturnStatement(newInstanceVar));

                        javaMethod.addStatement(b);
View Full Code Here

            // supercombinator.
            // private static final (typeConsType) self = new ThisClass();
            int modifiers = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
            JavaExpression selfInitializer;
            if (LECCMachineConfiguration.passExecContextToDataConstructors()) {
                selfInitializer = new ClassInstanceCreationExpression(className, JavaExpression.LiteralWrapper.NULL, JavaTypeNames.RTEXECUTION_CONTEXT);
            } else {
                selfInitializer = new ClassInstanceCreationExpression(className);
            }
            JavaFieldDeclaration selfFieldDeclaration = new JavaFieldDeclaration(modifiers, className, instanceName, selfInitializer);
            javaClassRep.addFieldDeclaration(selfFieldDeclaration);

        }
View Full Code Here

                        }
                    }
                }

                // return new ThisClass($arg0, $arg1, ...);
                final JavaExpression cice = new ClassInstanceCreationExpression(className, arguments, argTypes);

                javaMethod.addStatement(new ReturnStatement(cice));
            } else {
                // return new ThisClass();
                final JavaExpression cice = new ClassInstanceCreationExpression(className, JavaExpression.LiteralWrapper.NULL, JavaTypeNames.RTEXECUTION_CONTEXT);
                javaMethod.addStatement(new ReturnStatement(cice));
            }
        }
View Full Code Here

                        constructorArgTypes[i] = SCJavaDefn.typeExprToTypeName(fieldTypes[i]);
                    }
                }
            }

            JavaExpression cc = new ClassInstanceCreationExpression (className, constructorArgs, constructorArgTypes);

            javaMethod.addStatement(new ReturnStatement(cc));
        }
View Full Code Here

TOP

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

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.