Initial cleaning up in compiler, fixing bugs
[iot2.git] / iotjava / iotpolicy / IoTCompiler.java
index acd0a62602240c74e5e577316ec2a5d38d034a01..4450f1d3c74a7e39b0a00069a92c158e9bb266cc 100644 (file)
@@ -23,6 +23,8 @@ import iotpolicy.tree.CapabilityDecl;
 import iotpolicy.tree.InterfaceDecl;
 import iotpolicy.tree.RequiresDecl;
 
+import iotrmi.Java.IoTRMITypes;
+
 
 /** Class IoTCompiler is the main interface/stub compiler for
  *  files generation. This class calls helper classes
@@ -55,97 +57,6 @@ public class IoTCompiler {
         */
        private final static String OUTPUT_DIRECTORY = "output_files";
 
-       /**
-        * Primitive data types
-        */
-       private final static String[] primitives = new String[] {
-
-               "byte",
-               "Byte",
-               "short",
-               "Short",
-               "int",
-               "Integer",
-               "long",
-               "Long",
-               "float",
-               "Float",
-               "double",
-               "Double",
-               "boolean",
-               "Boolean",
-               "char",
-               "Character",
-               "string",
-               "String",
-               "void"
-       };
-
-       /**
-        * Primitive data types in C++ to map the primitives list
-        */
-       private final static String[] primitivesCplus = new String[] {
-
-               "char",
-               "char",
-               "short",
-               "short",
-               "int",
-               "int",
-               "long",
-               "long",
-               "float",
-               "float",
-               "double",
-               "double",
-               "bool",
-               "bool",
-               "char",
-               "char",
-               "string",
-               "string",
-               "void"
-       };
-
-       /**
-        * Non-primitive data types supported by this compiler
-        */
-       private final static String[] nonPrimitives = new String[] {
-
-               "Set",
-               "HashSet",
-               "Map",
-               "HashMap",
-               "List",
-               "ArrayList"
-       };
-
-       /**
-        * Non-primitive Java libraries based on the list above
-        */
-       private final static String[] nonPrimitiveJavaLibs = new String[] {
-
-               "java.util.Set",
-               "java.util.HashSet",
-               "java.util.Map",
-               "java.util.HashMap",
-               "java.util.List",
-               "java.util.ArrayList"
-       };
-
-       /**
-        * Non-primitive C++ libraries based on the list above
-        */
-       private final static String[] nonPrimitiveCplusLibs = new String[] {
-
-               "set",
-               "unordered_set",
-               "map",
-               "unordered_map",
-               "list",
-               "list"
-       };
-
        private enum ParamCategory {
 
                PRIMITIVES,             // All the primitive types, e.g. byte, short, int, long, etc.
@@ -162,11 +73,11 @@ public class IoTCompiler {
                mapIntDeclHand = new HashMap<String,DeclarationHandler>();
                mapInt2NewInts = new HashMap<String,Map<String,Set<String>>>();
                mapPrimitives = new HashMap<String,String>();
-                       arraysToMap(mapPrimitives, primitives, primitivesCplus);
+                       arraysToMap(mapPrimitives, IoTRMITypes.primitivesJava, IoTRMITypes.primitivesCplus);
                mapNonPrimitivesJava = new HashMap<String,String>();
-                       arraysToMap(mapNonPrimitivesJava, nonPrimitives, nonPrimitiveJavaLibs);
+                       arraysToMap(mapNonPrimitivesJava, IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitiveJavaLibs);
                mapNonPrimitivesCplus = new HashMap<String,String>();
-                       arraysToMap(mapNonPrimitivesCplus, nonPrimitives, nonPrimitiveCplusLibs);
+                       arraysToMap(mapNonPrimitivesCplus, IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
                pw = null;
                dir = OUTPUT_DIRECTORY;
                subdir = null;
@@ -245,21 +156,6 @@ public class IoTCompiler {
                }
                // Map the map of interface-methods to the original interface
                mapInt2NewInts.put(origInt, mapNewIntMethods);
-
-/*             for (String origint : mapInt2NewInts.keySet()) {
-
-                       System.out.println("Original Interface: " + origint);
-                       Map<String,Set<String>> mapNewInt = mapInt2NewInts.get(origint);
-                       for (String intf : mapNewInt.keySet()) {
-
-                               System.out.println("\tNew Interface: " + intf);
-                               Set<String> methods = mapNewInt.get(intf);
-                               for (String meth : methods) {
-
-                                       System.out.println("\t\tMethod: " + meth);
-                               }
-                       }
-               }*/
        }
 
 
@@ -298,7 +194,7 @@ public class IoTCompiler {
                                for (int i = 0; i < methParams.size(); i++) {
                                        // Check for params with driver class types and exchange it 
                                        //              with its remote interface
-                                       String paramType = checkAndGetParamClass(methPrmTypes.get(i));
+                                       String paramType = checkAndGetParamClass(methPrmTypes.get(i), false);
                                        print(paramType + " " + methParams.get(i));
                                        // Check if this is the last element (don't print a comma)
                                        if (i != methParams.size() - 1) {
@@ -336,7 +232,10 @@ public class IoTCompiler {
                        DeclarationHandler decHandler = mapIntDeclHand.get(intface);
                        InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
                        List<String> methods = intDecl.getMethods();
+
+                       // DEBUGGG
                        Set<String> includeClasses = getIncludeClasses(methods, intDecl);
+
                        printIncludeStatements(includeClasses);
                        println("");
                        println("using namespace std;");
@@ -354,9 +253,12 @@ public class IoTCompiler {
                                for (int i = 0; i < methParams.size(); i++) {
                                        // Check for params with driver class types and exchange it 
                                        //              with its remote interface
-                                       String paramType = checkAndGetParamClass(methPrmTypes.get(i));
+                                       String paramType = checkAndGetParamClass(methPrmTypes.get(i), true);
                                        paramType = checkAndGetCplusType(paramType);
-                                       print(paramType + " " + methParams.get(i));
+                                       // Check for arrays - translate into vector in C++
+                                       String paramComplete = checkAndGetCplusArray(paramType, methParams.get(i));
+                                       //print(paramType + " " + param);
+                                       print(paramComplete);
                                        // Check if this is the last element (don't print a comma)
                                        if (i != methParams.size() - 1) {
                                                print(", ");
@@ -405,7 +307,9 @@ public class IoTCompiler {
                                        print("public " + intDecl.getMethodType(method) + " " +
                                                intDecl.getMethodId(method) + "(");
                                        for (int i = 0; i < methParams.size(); i++) {
-                                               print(methPrmTypes.get(i) + " " + methParams.get(i));
+                                               String paramType = checkAndGetParamClass(methPrmTypes.get(i), false);
+                                               print(paramType + " " + methParams.get(i));
+                                               //print(methPrmTypes.get(i) + " " + methParams.get(i));
                                                // Check if this is the last element (don't print a comma)
                                                if (i != methParams.size() - 1) {
                                                        print(", ");
@@ -460,9 +364,10 @@ public class IoTCompiler {
                                        print("virtual " + convertType(intDecl.getMethodType(method)) + " " +
                                                intDecl.getMethodId(method) + "(");
                                        for (int i = 0; i < methParams.size(); i++) {
-
-                                               String methPrmType = checkAndGetCplusType(methPrmTypes.get(i));
-                                               print(methPrmType + " " + methParams.get(i));
+                                               String methPrmType = checkAndGetParamClass(methPrmTypes.get(i), true);
+                                               methPrmType = checkAndGetCplusType(methPrmType);
+                                               String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
+                                               print(methParamComplete);
                                                // Check if this is the last element (don't print a comma)
                                                if (i != methParams.size() - 1) {
                                                        print(", ");
@@ -571,9 +476,9 @@ public class IoTCompiler {
                                        print(convertType(intDecl.getMethodType(method)) + " " +
                                                intDecl.getMethodId(method) + "(");
                                        for (int i = 0; i < methParams.size(); i++) {
-
                                                String methPrmType = checkAndGetCplusType(methPrmTypes.get(i));
-                                               print(methPrmType + " " + methParams.get(i));
+                                               String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
+                                               print(methParamComplete);
                                                // Check if this is the last element (don't print a comma)
                                                if (i != methParams.size() - 1) {
                                                        print(", ");
@@ -611,11 +516,11 @@ public class IoTCompiler {
                        methType.equals("double")) {
 
                        return "1";
-               } else if ( methType.equals("String") ||
-                                       methType.equals("byte")) {
+               } else if ( methType.equals("String")) {
   
                        return "\"a\"";
-               } else if ( methType.equals("char")) {
+               } else if ( methType.equals("char") ||
+                                       methType.equals("byte")) {
 
                        return "\'a\'";
                } else if ( methType.equals("boolean")) {
@@ -679,9 +584,9 @@ public class IoTCompiler {
        }
 
 
-       /**================================================
-        * Helper functions to write stub codes into files
-        **================================================
+       /**================
+        * Helper functions
+        **================
         */
        boolean newline=true;
        int tablevel=0;
@@ -844,11 +749,18 @@ public class IoTCompiler {
                for (String method : methods) {
 
                        List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
-                       for (String paramType : methPrmTypes) {
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       for (int i = 0; i < methPrmTypes.size(); i++) {
 
-                               String simpleType = getSimpleType(paramType);
+                               String simpleType = getSimpleType(methPrmTypes.get(i));
+                               String param = methParams.get(i);
                                if (getParamCategory(simpleType) == ParamCategory.NONPRIMITIVES) {
-                                       includeClasses.add(getNonPrimitiveCplusClass(simpleType));
+                                       includeClasses.add("<" + getNonPrimitiveCplusClass(simpleType) + ">");
+                               } else if (getParamCategory(simpleType) == ParamCategory.USERDEFINED) {
+                                       includeClasses.add("\"" + exchangeParamType(simpleType) + ".hpp\"");
+                               } else if (param.contains("[]")) {
+                               // Check if this is array for C++; translate into vector
+                                       includeClasses.add("<vector>");
                                }
                        }
                }
@@ -867,7 +779,7 @@ public class IoTCompiler {
        private void printIncludeStatements(Set<String> includeClasses) {
 
                for(String cls : includeClasses) {
-                       println("#include <" + cls + ">");
+                       println("#include " + cls);
                }
        }
 
@@ -910,13 +822,33 @@ public class IoTCompiler {
        }
 
 
+       // Detect array declaration, e.g. int A[],
+       //              then generate "int A[]" in C++ as "vector<int> A"
+       private String checkAndGetCplusArray(String paramType, String param) {
+
+               String paramComplete = null;
+               // Check for array declaration
+               if (param.contains("[]")) {
+                       paramComplete = "vector<" + paramType + "> " + param.replace("[]","");
+               } else
+                       // Just return it as is if it's not an array
+                       paramComplete = paramType + " " + param;
+
+               return paramComplete;
+       }
+
+
        // Get simple types, e.g. HashSet for HashSet<...>
        // Basically strip off the "<...>"
-       private String checkAndGetParamClass(String paramType) {
+       private String checkAndGetParamClass(String paramType, boolean needPtr) {
 
                // Check if this is generics
                if(getParamCategory(paramType) == ParamCategory.USERDEFINED) {
-                       return exchangeParamType(paramType);
+                       // If true then return with pointer (C++)
+                       if (needPtr)
+                               return exchangeParamType(paramType) + "*";
+                       else    // Java, so no pointer needed
+                               return exchangeParamType(paramType);
                } else
                        return paramType;
        }
@@ -945,7 +877,11 @@ public class IoTCompiler {
                } else {
                // NULL value - this means policy files missing
                        throw new Error("IoTCompiler: Parameter type lookup failed for " + intface +
-                               "... Please provide the necessary policy files for user-defined types.");
+                               "... Please provide the necessary policy files for user-defined types." +
+                               " If this is an array please type the brackets after the variable name," +
+                               " e.g. \"String str[]\", not \"String[] str\"." +
+                               " If this is a Collections (Java) / STL (C++) type, this compiler only" +
+                               " supports List/ArrayList (Java) or list (C++).");
                }
        }