From: rtrimana <rtrimana@uci.edu>
Date: Tue, 8 Nov 2016 19:55:04 +0000 (-0800)
Subject: Initial cleaning up in compiler, fixing bugs
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=2a853e3c4ad334c03378e28c771e32b4a4dbc758;p=iot2.git

Initial cleaning up in compiler, fixing bugs
---

diff --git a/iotjava/Makefile b/iotjava/Makefile
index bd1c555..feafe51 100644
--- a/iotjava/Makefile
+++ b/iotjava/Makefile
@@ -27,6 +27,20 @@ PHONY += runtime
 runtime:
 	$(JAVAC) -classpath . iotruntime/master/*.java -d $(BIN_DIR)
 
+# TODO: Can remove this later - just to test-compile the resulted files from the compiler
+PHONY += compile
+compile:
+	cd $(BIN_DIR)/iotpolicy/output_files; cp *.java ./Java
+	cd $(BIN_DIR)/iotpolicy/output_files; cp *.hpp ./Cplus
+	cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:.. *.java
+	cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./*.hpp --std=c++11 -pthread -pg
+
+PHONY += clean
+clean:
+	cd $(BIN_DIR)/iotpolicy/output_files; rm -rf *.class *.gch
+	cd $(BIN_DIR)/iotpolicy/output_files/Java; rm -rf *.class
+	cd $(BIN_DIR)/iotpolicy/output_files/Cplus; rm -rf *.gch
+
 # RMI compilation and run
 PHONY += rmi
 rmi:
diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java
index 4bbeb6a..4450f1d 100644
--- a/iotjava/iotpolicy/IoTCompiler.java
+++ b/iotjava/iotpolicy/IoTCompiler.java
@@ -194,10 +194,8 @@ 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 paramComplete = checkAndGetCplusArray(paramType, methParams.get(i));
-					//print(paramType + " " + methParams.get(i));
-					print(paramComplete);
+					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) {
 						print(", ");
@@ -234,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;");
@@ -252,7 +253,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), true);
 					paramType = checkAndGetCplusType(paramType);
 					// Check for arrays - translate into vector in C++
 					String paramComplete = checkAndGetCplusArray(paramType, methParams.get(i));
@@ -306,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(", ");
@@ -361,10 +364,9 @@ 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));
+						String methPrmType = checkAndGetParamClass(methPrmTypes.get(i), true);
+						methPrmType = checkAndGetCplusType(methPrmType);
 						String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
-						//print(methPrmType + " " + methParam);
 						print(methParamComplete);
 						// Check if this is the last element (don't print a comma)
 						if (i != methParams.size() - 1) {
@@ -471,17 +473,11 @@ public class IoTCompiler {
 
 					List<String> methParams = intDecl.getMethodParams(method);
 					List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
-
-					//System.out.println("\n\nMethod param: " + intDecl.getMethodParams(method));
-					//System.out.println("\n\nMethod param type: " + intDecl.getMethodParamTypes(method));
-					//System.out.println("\n\n");
-
 					print(convertType(intDecl.getMethodType(method)) + " " +
 						intDecl.getMethodId(method) + "(");
 					for (int i = 0; i < methParams.size(); i++) {
 						String methPrmType = checkAndGetCplusType(methPrmTypes.get(i));
 						String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
-						//print(methPrmType + " " + methParam);
 						print(methParamComplete);
 						// Check if this is the last element (don't print a comma)
 						if (i != methParams.size() - 1) {
@@ -520,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")) {
@@ -759,10 +755,12 @@ public class IoTCompiler {
 				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");
+					includeClasses.add("<vector>");
 				}
 			}
 		}
@@ -781,7 +779,7 @@ public class IoTCompiler {
 	private void printIncludeStatements(Set<String> includeClasses) {
 
 		for(String cls : includeClasses) {
-			println("#include <" + cls + ">");
+			println("#include " + cls);
 		}
 	}
 
@@ -842,11 +840,15 @@ public class IoTCompiler {
 
 	// 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;
 	}