Package org.openquark.cal.compiler

Examples of org.openquark.cal.compiler.TypeConstructor


        typeConstructorsClass.setJavaDoc(jdc);

        // Build up a list of type constructors and sort by name.
        List<String> typeConstructorNames = new ArrayList<String>();
        for (int i = 0, n = moduleTypeInfo.getNTypeConstructors(); i < n; ++i) {
            TypeConstructor typeCons = moduleTypeInfo.getNthTypeConstructor(i);
            if (typeCons.getScope().isPublic() != publicEntities) {
                continue;
            }
            typeConstructorNames.add(typeCons.getName().getUnqualifiedName());
        }
        Collections.sort(typeConstructorNames);
       
        if (typeConstructorNames.size() == 0) {
            return;
        }
       
        bindingClass.addInnerClass(typeConstructorsClass);
       
        // For each type constructor create a QualifiedName field.
        // The field will have the same name as the type constructor.
        for (final String typeConstructorName : typeConstructorNames) {
           
            TypeConstructor typeCons = moduleTypeInfo.getTypeConstructor(typeConstructorName);
            // Add a field for the typeConstructor name.
            // 'static final QualifiedName typeConstructorName = QualifiedName.make(moduleName, typeConstructorName);
            // We need to check for conflict between the functionName
            // and java keywords.  It is valid to have a CAL
            // function called assert but not valid to have java code
            // 'static final String assert = "assert";
            String fieldName = fixupVarName(typeConstructorName);
           
            // Since TypeConsApp names are capitalized it is possible to have
            // a conflict between the name of the field and the name of the top
            // level class.
            if (fieldName.equals(this.bindingClassName)) {
                fieldName = fieldName + "_";
            }
           
            JavaFieldDeclaration jfd =
                makeQualifiedNameDeclaration(fieldName, typeConstructorName);

            // Add JavaDoc for the field declaration.  We use the CALDoc if there is any.
            JavaDocComment typeConstructorComment;
            CALDocComment cdc = typeCons.getCALDocComment();
            if (cdc != null) {
                typeConstructorComment = new JavaDocComment(calDocCommentToJavaComment(cdc, null, false));
            } else {
                typeConstructorComment = new JavaDocComment("/** Name binding for TypeConsApp: " + typeConstructorName + ". */");
            }
View Full Code Here


        dataConstructorsClass.setJavaDoc(jdc);

        // We want to build up and sort a list of type constructor names.
        List<String> typeConstructorNames = new ArrayList<String>();
        for (int i = 0, n = moduleTypeInfo.getNTypeConstructors(); i < n; ++i) {
            TypeConstructor typeCons = moduleTypeInfo.getNthTypeConstructor(i);
            typeConstructorNames.add(typeCons.getName().getUnqualifiedName());
        }
        Collections.sort(typeConstructorNames);

       
        /*
         * Create a method to generate a SourceModel expression which will result
         * in the creation of an instance of the DC.
         * Create a QualifiedName field for the name of the DC.
         *
         * The ordering will be by name of the type constructor and then by dc ordinal.
         */
        boolean addClass = false;
       
        // Set of String used to avoid naming conflicts in the generated fields.
        // We want to build up a set of all the names that will be used for the methods and
        // QualifiedName fields.  This will allow us to avoid naming conflicts when creating
        // the ordinal fields.
        Set<String> javaNames = new HashSet<String>();
        for (final String typeConstructorName : typeConstructorNames) {
            TypeConstructor typeCons = moduleTypeInfo.getTypeConstructor(typeConstructorName);
            for (int i = 0, n = typeCons.getNDataConstructors(); i < n; ++i) {
                DataConstructor dc = typeCons.getNthDataConstructor(i);
               
                // If we are showing public entities we only car about public data constructors.
                if (dc.getScope().isPublic() != publicEntities) {
                    continue;
                }

                // This is the name used to name the java function and the QualfiedName field
                // associated with this data constructor.
                String javaFuncName = fixupVarName(dc.getName().getUnqualifiedName());
                javaNames.add(javaFuncName);
            }
        }
       
        for (final String typeConstructorName : typeConstructorNames) {
            TypeConstructor typeCons = moduleTypeInfo.getTypeConstructor(typeConstructorName);
            boolean commentAdded = false;
            for (int i = 0, n = typeCons.getNDataConstructors(); i < n; ++i) {
                DataConstructor dc = typeCons.getNthDataConstructor(i);
               
                // If we are showing public entities we only car about public data constructors.
                if (dc.getScope().isPublic() != publicEntities) {
                    continue;
                }
               
                addClass = true;
               
                // If this is the first DC for the data type we want a non JavaDoc comment
                // indicating the data type.
                if (!commentAdded) {
                    MultiLineComment mlc = new MultiLineComment("DataConstructors for the " + typeCons.getName().getQualifiedName() + " data type.");
                    dataConstructorsClass.addComment(mlc);
                    commentAdded = true;
                }

                // Get the types of the data constructor fields.
View Full Code Here

        TypeConsApp typeConsApp = typeExpr.rootTypeConsApp();
        if (typeConsApp == null) {
            return false;
        }

        TypeConstructor typeCons = typeConsApp.getRoot();

        for (int i = 0, n = typeCons.getNDataConstructors(); i < n; ++i) {
            DataConstructor dc = typeCons.getNthDataConstructor(i);
            TypeExpr[] fieldTypes = SCJavaDefn.getFieldTypesForDC(dc);
            for (int j = 0, k = fieldTypes.length; j < k; ++j) {
                TypeExpr fieldType = fieldTypes[j];
                TypeConsApp fieldTc = fieldType.rootTypeConsApp();
                if (fieldTc != null &&
View Full Code Here

        Block switchBlock = new Block();

        // Extract the alternatives
        Expression.Switch.SwitchAlt[] alts = eswitch.getAlts();

        TypeConstructor typeCons = null;
        boolean isEnumDataType = false;
        for (int i = 0; i < alts.length; ++i) {
            if (!alts[i].isDefaultAlt()) {
                DataConstructor dc = (DataConstructor)alts[i].getFirstAltTag();
                typeCons = dc.getTypeConstructor();
                isEnumDataType = SCJavaDefn.isEnumDataType(dc);
                break;
            }
        }

        if (typeCons == null) {
            throw new CodeGenerationException ("Unable to retrieve TypeConstructor for switch in " + getFunctionName() + ".");
        }



        int nDataConstructorsForType = typeCons.getNDataConstructors();
        if (nDataConstructorsForType == 0) {
            throw new CodeGenerationException ("Encountered a data type with zero data constructors in a switch in " + getFunctionName() + ".");
        }

        DataConstructor[] allDCs = new DataConstructor [nDataConstructorsForType];
        for (int i = 0; i < nDataConstructorsForType; ++i ) {
            DataConstructor dc = typeCons.getNthDataConstructor(i);
            allDCs[dc.getOrdinal()] = dc;
        }

        // If all the case alternatives return a boolean literal we may
        // be able to optimize this.
View Full Code Here

     * @param dc
     * @param module
     * @return true if the data constructor is a TagDC
     */
    static boolean isTagDC (DataConstructor dc, LECCModule module) {
        TypeConstructor typeCons = dc.getTypeConstructor();

        int nTagDCs = 0;
        for (int i = 0; i < typeCons.getNDataConstructors(); ++i) {
            DataConstructor dci = typeCons.getNthDataConstructor(i);
            if (dci.getArity() == 0) {
                nTagDCs++;
            }
        }
        return nTagDCs > 1;
View Full Code Here

                if (visibleDataConstructors == null || visibleDataConstructors.length == 0) {

                    // Include non-visible constructors if there are no visible ones.
                   
                    ModuleTypeInfo moduleTypeInfo = getPerspective().getMetaModule(typeConsName.getModuleName()).getTypeInfo();
                    TypeConstructor typeCons = moduleTypeInfo.getTypeConstructor(typeConsName.getUnqualifiedName());
                   
                    for (int n = 0, count = typeCons.getNDataConstructors(); n < count; n++) {
                       
                        DataConstructor dataCons = typeCons.getNthDataConstructor(n);
                        int nameLength = fontMetrics.stringWidth(dataCons.getAdaptedName(namingPolicy));
                        if (nameLength > fixedWidth) {
                            fixedWidth = nameLength;
                        }
                    }
View Full Code Here

        Set<ModuleName> mustHaveImports = new LinkedHashSet<ModuleName>();
        Map<String, TypeSignature> castFunctions = new LinkedHashMap<String, TypeSignature>();
        Map<QualifiedName, TypeConstructor> additionalForeignTypeDeclarations = new LinkedHashMap<QualifiedName, TypeConstructor>();
       
        for (int i = 0, n = module.getNTypeConstructors(); i < n; ++i) {
            TypeConstructor tc = module.getNthTypeConstructor(i);
            if (typeConstructorsToIgnore.contains(tc.getName()) ||
                tc.getForeignTypeInfo() != null) {
                logMessage(Level.INFO, "Skipped type constructor " + tc.getName().getUnqualifiedName() + " because of ignore directive.");
                continue;
            }
           
            // If the type constructor is declared as inputable/outputable in the containing module
            // we skip it because generating inputable/outputable instances would conflict.
            boolean alreadyInputableOutputable = false;
            for (int j = 0, k = module.getNClassInstances(); j < k; ++j) {
                ClassInstance ci = module.getNthClassInstance(j);
                if (ci.getTypeClass().getName().equals(CAL_Prelude.TypeClasses.Inputable) ||
                    ci.getTypeClass().getName().equals(CAL_Prelude.TypeClasses.Outputable)) {
                    // Check the instance type
                    TypeExpr instanceTypeExpr = ci.getType();
                    TypeConsApp tca = instanceTypeExpr.rootTypeConsApp();
                    if (tca != null) {
                        if (tca.getRoot().getName().equals(tc.getName())) {
                            alreadyInputableOutputable = true;
                        }
                    }
                }
            }
            if (alreadyInputableOutputable) {
                logMessage(Level.INFO, "Skipped type constructor " + tc.getName().getUnqualifiedName() + " because it is alreayd an instance of Inputable or Outputable.");
                continue;
            }
           
            // Build up info about the type constructor and data constructors.
            TypeConstructorInfo typeConstructorInfo =
                getTypeConstructorInfo (tc, typeToClassMappings, moduleToPackageMappings, module, targetPackage);
           
           
            // Now we want to generate the Java class.
            JavaDataClassGenerator javaDataClassGenerator =
                new JavaDataClassGenerator(
                        targetPackage,
                        typeConstructorInfo);
           
            JavaClassRep javaClass = javaDataClassGenerator.generateClassForType();
            classReps.put(tc.getName(), javaClass);
            classLogMessages.put(tc.getName(), javaDataClassGenerator.logMessages);
           
            // Generate the CAL I/O
            CAL_IO_Generator ioGenerator =
                new CAL_IO_Generator (
                        typeConstructorInfo,
                        module,
                        javaClass);
           
            Collection<TopLevelSourceElement> topLevelElements = ioGenerator.generateCAL_IO ();
            allTopLevelElements.addAll(topLevelElements);
            calLogMessages.addAll(ioGenerator.logMessages);
           
            mustHaveImports.addAll(ioGenerator.getInputableImports());
            castFunctions.putAll(ioGenerator.castFunctionNameToTypeSignature);
            additionalForeignTypeDeclarations.putAll(ioGenerator.additionalForeignTypeDeclarations);
        }
       
        SourceModel.ModuleDefn moduleDefn = null;
        if (allTopLevelElements.size() > 0) {
           
            ModuleName newModuleName = ModuleName.make(module.getModuleName().toString() + "_JavaIO");
           
            for (Map.Entry<String, TypeSignature> entry : castFunctions.entrySet()) {
                String castFunctionName = (String)entry.getKey();
                SourceModel.TypeSignature typeSignature = (TypeSignature)entry.getValue();
               
                TypeExprDefn domain = ((TypeExprDefn.Function)typeSignature.getTypeExprDefn()).getDomain();
                TypeExprDefn coDomain = ((TypeExprDefn.Function)typeSignature.getTypeExprDefn()).getCodomain();

                SourceModel.CALDoc.TextSegment.Plain textSegment =
                    SourceModel.CALDoc.TextSegment.Plain.make(
                            "Cast an instance of " + domain.toString() + " to " + coDomain.toString());
               
                SourceModel.CALDoc.TextBlock textBlock =
                    SourceModel.CALDoc.TextBlock.make(new SourceModel.CALDoc.TextSegment.TopLevel[]{textSegment});
                   
                SourceModel.CALDoc.Comment.Function functionComment =
                    SourceModel.CALDoc.Comment.Function.make(
                            textBlock,
                            null);
               
                SourceModel.FunctionDefn castFunction =
                    FunctionDefn.Foreign.make(
                            functionComment,
                            castFunctionName,
                            Scope.PRIVATE,
                            true,
                            "cast",
                            typeSignature);
                allTopLevelElements.add(castFunction);
            }

            for (Map.Entry<QualifiedName, TypeConstructor>entry : additionalForeignTypeDeclarations.entrySet()) {
                QualifiedName typeConstructorName = (QualifiedName)entry.getKey();
                TypeConstructor typeConstructor = (TypeConstructor)entry.getValue();
               
                // Declare the java class as a foreign type.
                // ex. data foreign unsafe import jvm "org.olap.CubeType" public JCubeType;
                SourceModel.TypeConstructorDefn.ForeignType foreignType =
                    TypeConstructorDefn.ForeignType.make(
                            typeConstructorName.getUnqualifiedName(),
                            Scope.PRIVATE,
                            ForeignTypeInfo.getCalSourceName(typeConstructor.getForeignTypeInfo().getForeignType()),
                            Scope.PRIVATE,
                            null);
                allTopLevelElements.add(foreignType);
            }
           
View Full Code Here

                containsFields);
    }
   
    private String getNameOfCALForeignType (JavaTypeName javaType, ModuleTypeInfo module) throws UnableToResolveForeignEntityException {
        // First try to find a type constructor for each java type;
        TypeConstructor typeConstructor = findTypeConstructorForJavaClass(javaType, module);
       
        if (typeConstructor != null) {
            if (typeConstructor.getScope() == Scope.PUBLIC) {
                return typeConstructor.getName().toString();
            } else {
                return typeConstructor.getName().getUnqualifiedName();
            }
        } else
        if (javaType.equals(JavaTypeName.BOOLEAN)){
            return CAL_Prelude.TypeConstructors.Boolean.toString();
        } else {
View Full Code Here

        }
       
        visitedModules.add(module);
       
        for (int i = 0, n = module.getNTypeConstructors(); i < n; ++i) {
            TypeConstructor tc = module.getNthTypeConstructor(i);
           
            ForeignTypeInfo fti = tc.getForeignTypeInfo();
            if (fti == null) {
                continue;
            }
           
            JavaTypeName foreignType = JavaTypeName.make(fti.getForeignType());
            if (javaType.equals(foreignType)) {
                return tc;
            }
        }
       
        for (int i = 0, n = module.getNImportedModules(); i < n; ++i) {
            ModuleTypeInfo importedModule = module.getNthImportedModule(i);
            TypeConstructor tc = findTypeConstructorForJavaClass(javaType, importedModule, visitedModules);
            if (tc != null) {
                return tc;
            }
        }
       
View Full Code Here

                if(typeConsApp.getForeignTypeInfo() != null) {
                    ForeignTypeInfo fti = typeConsApp.getForeignTypeInfo();
                    return JavaTypeName.make (fti.getForeignType());
                }

                TypeConstructor typeConstructor = typeConsApp.getRoot();

                JavaTypeName typeName = (JavaTypeName)typeToClassMappings.get(typeConstructor.getName());
                if (typeName != null) {
                    return typeName;
                }

                String unqualifiedTypeConstructorName = typeConstructor.getName().getUnqualifiedName();
                if (unqualifiedTypeConstructorName.equals("Function")) {
                    return CAL_VALUE;
                }

                // We are going to build up a JavaTypeName based on our naming convention for generated
                // I/O classes.
               
                // Check to see if the CAL module is mapped to a Java package.
                String referencedPackage = moduleToPackageMappings.get(typeConstructor.getName().getModuleName());
                if (referencedPackage == null) {
                    // If not use the target package.
                    referencedPackage = targetPackage;
                }
                if (referencedPackage.endsWith(".")) {
                    referencedPackage = referencedPackage.substring(0, referencedPackage.lastIndexOf('.'));
                }
                if (referencedPackage.lastIndexOf('.') > 0) {
                    referencedPackage = referencedPackage.substring(0, referencedPackage.lastIndexOf('.')+1);
                } else {
                    referencedPackage = referencedPackage + ".";
                }
               
                // Create a Java class name.
                JavaTypeName typeConstructorClassName =
                    JavaTypeName.make(referencedPackage + typeConstructor.getName().getModuleName().getLastComponent() + "." + getClassName(typeConstructor), false);
                return typeConstructorClassName;
            }
        }
       
        return JavaTypeName.OBJECT;
View Full Code Here

TOP

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

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.