Package org.openquark.cal.compiler

Examples of org.openquark.cal.compiler.TypeExpr


        Class<?> handlerClass = valueNodeBuilderHelper.getValueNodeClass(newTypeExpr);

        if ((getClass().equals(handlerClass))) {
           
            // Transmute the endpoint nodes to the new types
            TypeExpr childTypeExpr = ((TypeConsApp)newTypeExpr).getArg(0);
            ValueNode leftNode;
            ValueNode rightNode;
            if (!childTypeExpr.sameType(this.leftNode.getTypeExpr())) {
                leftNode = this.leftNode.transmuteValueNode(valueNodeBuilderHelper, valueNodeTransformer, childTypeExpr);
                rightNode = this.rightNode.transmuteValueNode(valueNodeBuilderHelper, valueNodeTransformer, childTypeExpr);
            } else {
                leftNode = this.leftNode.copyValueNode();
                rightNode = this.rightNode.copyValueNode();
View Full Code Here


                } else {
                    accessApplication =
                        Expr.Application.make(new SourceModel.Expr[]{accessor, argCastToInnerJType});
                }
               
                TypeExpr fieldTypeExpr = dcFieldTypes[i];
                updateInputableImports(fieldTypeExpr);
               
                TypeExprDefn fieldTypeForGetter = getTypeExprDefn(new JavaTypeName[]{fieldType});
                String fieldTypeForGetterTypeConsName = ((TypeExprDefn.TypeCons)fieldTypeForGetter).getTypeConsName().toString();
                String dcFieldTypeConsName = "";
                TypeConsApp typeConsApp = fieldTypeExpr.rootTypeConsApp();
                if (typeConsApp != null) {
                    TypeConstructor tc = typeConsApp.getRoot();
                    dcFieldTypeConsName = tc.getName().toString();
                }
               
View Full Code Here

           
            for (int i = 0, n = module.getNClassInstances(); i < n; ++i) {
                ClassInstance ci = module.getNthClassInstance(i);
                if (ci.getTypeClass().getName().equals(className)) {
                    // Check the instance type
                    TypeExpr instanceTypeExpr = ci.getType();
                    TypeConsApp tca = instanceTypeExpr.rootTypeConsApp();
                    if (tca != null) {
                        if (tca.getRoot().getName().equals(typeConstructorName)) {
                            return module;
                        }
                    }
View Full Code Here

                //
                // Since these require ordinal type arguments, "Range Boolean" will be
                // available as a value node, whereas "Range BinaryOp" will not.
               
                // Get the more specialized type required by the construction sc (this will be "Ord a => a").
                TypeExpr scArgType = getValueNodeBuilderHelper().getPerspective().getWorkingModuleTypeInfo().getVisibleFunction(RANGE_CONSTRUCTION_SC_NAME).getTypeExpr().getTypePieces()[0];
               
                // Get the argument type of the value node type expression (this will be "Boolean" for "Range Boolean")
                TypeExpr endpointType = ((TypeConsApp)typeExpr).getArg(0);
               
                if (!(endpointType instanceof TypeVar)) {
                    // If the type is specialized, check if the type is valid by unifying with
                    // the construction sc argument type.
                    if (!TypeExpr.canUnifyType(scArgType, endpointType, getValueNodeBuilderHelper().getPerspective().getWorkingModuleTypeInfo())) {
View Full Code Here

        checkCopyType(newTypeExpr);
        List<ValueNode> copyList = null;
        if (listValue != null) {
            copyList = new ArrayList<ValueNode>(listValue.size());
            TypeExpr componentTypeExpr = ListValueNode.getElementTypeExpr(newTypeExpr);
   
            for (int i = 0, listCount = listValue.size(); i < listCount; i++) {
   
                ValueNode elementVN = listValue.get(i);
                copyList.add(elementVN.copyValueNode(componentTypeExpr));
View Full Code Here

           
            // Go thru the element ValueNodes and transmute them.
            for (int i = 0, elementCount = getNElements(); i < elementCount; i++) {
           
                ValueNode elementVN = getValueAt(i);
                TypeExpr elementTypeExpr = ListValueNode.getElementTypeExpr(newTypeExpr);
                newComponentList.add(elementVN.transmuteValueNode(valueNodeBuilderHelper, valueNodeTransformer, elementTypeExpr));
            }
           
            ValueNode newValueNode = valueNodeBuilderHelper.buildValueNode(newComponentList, null, newTypeExpr);
           
View Full Code Here

            }
           
            index++;
        }       
       
        TypeExpr switchedType = newValueNode.getTypeExpr();       
                   
        ModuleTypeInfo currentModuleTypeInfo = valueNodeBuilderHelper.getPerspective().getWorkingModuleTypeInfo();
                   
        TypeExpr[] updatedSpecializedTypes = TypeExpr.getUpdatedSpecializedTypes(switchedType, switchIndex, oldSpecializedTypes, unconstrainedTypes, currentModuleTypeInfo);
        if (updatedSpecializedTypes == null) {
            //this is not a valid type switch. Perhaps there should be better validation here.
            throw new IllegalStateException();
        }
                                          
        // Construct a map giving the new values.
        Map<ValueNode, ValueNode> oldToNewValueMap = new HashMap<ValueNode, ValueNode>();
       
        // Iterate through the affected values.
        for (int i = 0; i < nTypes; ++i) {           
       
            ValueNode valueNode = oldValueNodes[i];
            TypeExpr updatedSpecializedType = updatedSpecializedTypes[i];

            ValueNode newValue;
            if (i == switchIndex) {
                // Make a copy of the value node which changed, ensuring that it has the updated type.
                newValue = newValueNode.copyValueNode()//updatedSpecializedType);
View Full Code Here

           
            // Transmute all fields which have equal names, and create those which do not
            for (final FieldName newFieldName : newFieldNames) {
                ValueNode newFieldNode;
               
                TypeExpr newFieldType = recordType.getHasFieldType(newFieldName);
                int nameIndex = fieldNames.indexOf(newFieldName);
                if (nameIndex != -1) {
                   
                    // The record field in the new type is contained in our record
                    ValueNode oldFieldNode = this.fieldNodes.get(nameIndex);
View Full Code Here

            // Essentially this method builds an application
            // of the function to the supplied arguments using our
            // source model.
           
            // Build up the argument names and types.
            TypeExpr te = functionalAgent.getTypeExpr();
            int nArgs = te.getArity();
            int nNamedArgs = functionalAgent.getNArgumentNames();
           
            String paramNames[] = CALDocToJavaDocUtilities.getArgumentNamesFromCALDocComment(cdc, functionalAgent);
            if (paramNames.length != nArgs)  {
                throw new NullPointerException ("Mismatch between arity and number of arguments for " + calFuncName);
            }
            String origParamNames[] = new String[paramNames.length];
            System.arraycopy(paramNames, 0, origParamNames, 0, origParamNames.length);
           
            JavaTypeName paramTypes[] = new JavaTypeName [paramNames.length];
           
            // If we have named arguments we should use those names.
            Set<String> paramNamesSet = new HashSet<String>();
            for (int i = 0; i < paramNames.length; ++i) {
                String name = paramNames[i];

                if (name == null) {
                    if (i < nNamedArgs) {
                        name = functionalAgent.getArgumentName(i);
                    }
                   
                    if (name == null || name.equals("")) {
                        name = null;
                        // There is no name for this argument in the functional entity.  This
                        // usually occurs with foreign functions and class methods because
                        // you don't have to explicitly name the arguments.
                       
                        // Check to see if there was a CALDoc comment that named the arguments.
                        if (cdc != null && cdc.getNArgBlocks() == paramNames.length) {
                            name = cdc.getNthArgBlock(i).getArgName().getCalSourceForm();
                        }
                       
                        // If we still don't have an argument name just build one.
                        if (name == null) {
                            name = "arg_" + (i+1);
                        }
                    }
                   
                    // Make sure name is unique.
                    String baseName = name;
                    int suffix = 0;
                    while (paramNamesSet.contains(name)) {
                        name = baseName + "_" + suffix++;
                    }
                }
               
                origParamNames[i] = name;
                paramNames[i] = fixupVarName(name);
                paramTypes[i] = SOURCE_MODEL_EXPR_TYPE_NAME;
            }
           
            // Get the JavaDoc for the helper function.  If there is CALDoc
            // for the function we translate it to JavaDoc. 
            JavaDocComment funcComment;
            if (cdc != null) {
                funcComment = new JavaDocComment(calDocCommentToJavaComment(cdc, functionalAgent, false));
                funcComment = fixupJavaDoc(funcComment, origParamNames, paramNames);
            } else {
                funcComment = new JavaDocComment("Helper binding method for function: " + calFuncName + ". ");
                for (int iName = 0; iName < paramNames.length; ++iName) {
                    funcComment.addLine("@param " + paramNames[iName]);
                }
                funcComment.addLine("@return the SourceModule.expr representing an application of " + calFuncName);
            }
           
           
            // Create the method.
            JavaMethod bindingMethod =
                new JavaMethod(PUBLIC_STATIC_FINAL,
                               SOURCE_MODEL_EXPR_TYPE_NAME,
                               paramNames,
                               paramTypes,
                               null, javaFuncName);
            functionsClass.addMethod(bindingMethod);
           
            // Add the JavaDoc
            bindingMethod.setJavaDocComment(funcComment);
           
            // Build up the body of the method.
           
            // We want to us the QualifiedName field.
            JavaField nameField = new JavaField.Static(functionsClassTypeName, javaFuncName, QUALIFIED_NAME_TYPE_NAME);
           
            // Create an instance of SourceModel.Expr.Var for the CAL function.
            JavaExpression sourceModelVarCreation =
                new MethodInvocation.Static(
                        SOURCE_MODEL_EXPR_VAR_TYPE_NAME,
                        "make",
                        nameField,
                        QUALIFIED_NAME_TYPE_NAME,
                        SOURCE_MODEL_EXPR_VAR_TYPE_NAME);
           
            // For the comment for this method we want an @see referring to the previous method.
            String atSee = "@see #" + javaFuncName + "(";
            for (int iArg = 0; iArg < paramNames.length; ++iArg) {
                if (iArg == 0) {
                    atSee = atSee + paramTypes[iArg].getFullJavaSourceName();
                } else {
                    atSee = atSee + ", " + paramTypes[iArg].getFullJavaSourceName();
                }
            }
            atSee = atSee + ")";
           
            if (paramNames.length == 0) {
                // Simply return the Expr.Var
                bindingMethod.addStatement(new ReturnStatement(sourceModelVarCreation));
            } else {
                // Need to create an application.
               
                // Create an array of SourceModel.Expr.  The first element is the SourceModel.Expr.Var
                // for the CAL function the remaining elements are the function arguments.
                JavaExpression arrayElements[] = new JavaExpression[paramNames.length + 1];
                arrayElements[0] = sourceModelVarCreation;
               
                for (int i = 1; i <= paramNames.length; ++i) {
                    arrayElements[i] = new JavaExpression.MethodVariable(paramNames[i-1]);
                }
               
                // Build the array creation expression.
                JavaExpression arrayCreation =
                    new JavaExpression.ArrayCreationExpression(SOURCE_MODEL_EXPR_TYPE_NAME, arrayElements);
               
                // Invoke SourceModel.Expr.Application.make()
                MethodInvocation makeApply =
                    new MethodInvocation.Static(
                            SOURCE_MODEL_EXPR_APPLICATION_TYPE_NAME,
                            "make",
                            arrayCreation,
                            JavaTypeName.makeArrayType(SOURCE_MODEL_EXPR_TYPE_NAME),
                            SOURCE_MODEL_EXPR_APPLICATION_TYPE_NAME);
               
                bindingMethod.addStatement(new ReturnStatement (makeApply));
           
                // If any of the argument types correspond to a Java type.
                // (eg. Prelude.Int, Prelude.Long, etc. we can generate a version
                // of the binding function that takes these argument types.
                boolean primArgs = false;

                TypeExpr[] pieces = te.getTypePieces();
                for (int i = 0; i < paramNames.length; ++i) {
                    if (canTypeBeUnboxed(pieces[i])) {
                        primArgs = true;
                        // Change the param type.
                        paramTypes[i] = typeExprToTypeName(pieces[i]);
View Full Code Here

     * @param dc
     * @return an array of TypeExpr
     */
    private static TypeExpr[] getFieldTypesForDC (DataConstructor dc) {
        TypeExpr[] fieldTypes = new TypeExpr[dc.getArity()];
        TypeExpr dcType = dc.getTypeExpr();
        TypeExpr[] tft = dcType.getTypePieces(dc.getArity());
        System.arraycopy(tft, 0, fieldTypes, 0, fieldTypes.length);

        return fieldTypes;
    }
View Full Code Here

TOP

Related Classes of org.openquark.cal.compiler.TypeExpr

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.