X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=iotjava%2Fiotpolicy%2FIoTCompiler.java;h=3187637456d46161c1892f1d0abf607ecaf92e44;hb=ae77f0f97f70a6d3bef0faee0b7db27fb5e2849f;hp=1bd90c6d78259155aee65011d4bcdac8e00010cd;hpb=09bf1cf8e98526878b4732bf8d3077eb2d1c16e3;p=iot2.git diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 1bd90c6..3187637 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -943,66 +943,6 @@ public class IoTCompiler { } - /*private void writeStdMethodBodyJavaStub(InterfaceDecl intDecl, List methParams, - List methPrmTypes, String method) { - - checkAndWriteStructSetupJavaStub(methParams, methPrmTypes, intDecl, method); - println("int methodId = " + intDecl.getMethodNumId(method) + ";"); - String retType = intDecl.getMethodType(method); - println("Class retType = " + getSimpleType(getStructType(getEnumType(retType))) + ".class;"); - checkAndWriteEnumTypeJavaStub(methParams, methPrmTypes); - // Generate array of parameter types - if (isStructPresent(methParams, methPrmTypes)) { - writeStructParamClassJavaStub(methParams, methPrmTypes); - } else { - print("Class[] paramCls = new Class[] { "); - for (int i = 0; i < methParams.size(); i++) { - String paramType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); - print(getSimpleType(getEnumType(paramType)) + ".class"); - // Check if this is the last element (don't print a comma) - if (i != methParams.size() - 1) { - print(", "); - } - } - println(" };"); - // Generate array of parameter objects - print("Object[] paramObj = new Object[] { "); - for (int i = 0; i < methParams.size(); i++) { - print(getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i)); - // Check if this is the last element (don't print a comma) - if (i != methParams.size() - 1) { - print(", "); - } - } - println(" };"); - } - // Check if this is "void" - if (retType.equals("void")) { - println("rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);"); - } else { // We do have a return value - // Generate array of parameter types - if (isStructClass(getGenericType(getSimpleArrayType(retType)))) { - writeStructReturnJavaStub(getGenericType(getSimpleArrayType(retType)), retType); - } else { - // This is an enum type - if (getParamCategory(getGenericType(getSimpleArrayType(retType))) == ParamCategory.ENUM) { - println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);"); - checkAndWriteEnumRetTypeJavaStub(retType); - } else if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES) { - // Check if the return value NONPRIMITIVES - String retGenValType = getGenericType(retType); - println("Class retGenValType = " + retGenValType + ".class;"); - println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, retGenValType, paramCls, paramObj);"); - println("return (" + retType + ")retObj;"); - } else { - println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);"); - println("return (" + retType + ")retObj;"); - } - } - } - }*/ - - /** * HELPER: returnGenericCallbackType() returns the callback type */ @@ -1483,7 +1423,6 @@ public class IoTCompiler { String paramType = methPrmTypes.get(i); String param = methParams.get(i); if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object - println("try {"); String exchParamType = checkAndGetParamClass(getGenericType(paramType)); // Print array if this is array or list if this is a list of callback objects if (isArray(param)) { @@ -1521,17 +1460,24 @@ public class IoTCompiler { /** * HELPER: checkAndWriteEnumTypeJavaSkeleton() writes the enum type (convert from enum to int) */ - private void checkAndWriteEnumTypeJavaSkeleton(List methParams, List methPrmTypes) { + private void checkAndWriteEnumTypeJavaSkeleton(List methParams, List methPrmTypes, boolean isStructMethod) { + String offsetPfx = ""; + if (isStructMethod) + offsetPfx = "offset"; // Iterate and find enum declarations + boolean printed = false; for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); String simpleType = getGenericType(paramType); if (isEnumClass(simpleType)) { // Check if this is enum type - println("int paramInt" + i + "[] = (int[]) paramObj[" + i + "];"); - println(simpleType + "[] enumVals = " + simpleType + ".values();"); + println("int paramInt" + i + "[] = (int[]) paramObj[" + offsetPfx + i + "];"); + if (!printed) { + println(simpleType + "[] enumVals = " + simpleType + ".values();"); + printed = true; + } if (isArray(param)) { // An array println("int len" + i + " = paramInt" + i + ".length;"); println(simpleType + "[] paramEnum" + i + " = new " + simpleType + "[len" + i + "];"); @@ -1790,9 +1736,10 @@ public class IoTCompiler { List methPrmTypes, String method, boolean isCallbackMethod, String callbackType, boolean isStructMethod) { - checkAndWriteEnumTypeJavaSkeleton(methParams, methPrmTypes); + checkAndWriteEnumTypeJavaSkeleton(methParams, methPrmTypes, isStructMethod); Map mapStubParam = null; if (isCallbackMethod) { + println("try {"); mapStubParam = writeCallbackJavaStubGeneration(methParams, methPrmTypes, callbackType, isStructMethod); } // Check if this is "void" @@ -1965,9 +1912,10 @@ public class IoTCompiler { String param = methParams.get(i); String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { - if (!begin) { // Generate comma for not the beginning variable - print(", "); begin = false; - } + if (!begin) // Generate comma for not the beginning variable + print(", "); + else + begin = false; int methodNumId = intDecl.getMethodNumId(method); print("int struct" + methodNumId + "Size" + i); } @@ -2097,17 +2045,18 @@ public class IoTCompiler { List methParams = intDecl.getMethodParams(method); List methPrmTypes = intDecl.getMethodParamTypes(method); boolean structExist = false; + boolean begin = true; // Check for params with structs for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); String simpleType = getGenericType(paramType); - boolean begin = true; if (isStructClass(simpleType)) { structExist = true; - if (!begin) { - print(", "); begin = false; - } + if (!begin) + print(", "); + else + begin = false; int methodNumId = intDecl.getMethodNumId(method); print("struct" + methodNumId + "Size" + i); } @@ -2341,9 +2290,10 @@ public class IoTCompiler { String param = methParams.get(i); String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { - if (!begin) { // Generate comma for not the beginning variable - print(", "); begin = false; - } + if (!begin) // Generate comma for not the beginning variable + print(", "); + else + begin = false; int methodNumId = intDecl.getMethodNumId(method); print("int struct" + methodNumId + "Size" + i); } @@ -2724,7 +2674,6 @@ public class IoTCompiler { println(") { "); if (isCallbackMethod) writeCallbackMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method, callbackType); - //else writeStdMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method, callbackType, isCallbackMethod); println("}\n"); // Write the init callback helper method @@ -2762,90 +2711,14 @@ public class IoTCompiler { println("vecCallbackObj.push_back(skel" + i + ");"); if (isArrayOrList) println("}"); - } - } - print("int ___paramCB = "); - if (isArrayOrList) - println(callbackParam + ".size();"); - else - println("1;"); - } - -/* private void writeCallbackMethodBodyCplusStub(InterfaceDecl intDecl, List methParams, - List methPrmTypes, String method, String callbackType) { - - // Check if this is single object, array, or list of objects - boolean isArrayOrList = false; - String callbackParam = null; - for (int i = 0; i < methParams.size(); i++) { - - String paramType = methPrmTypes.get(i); - if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object - String param = methParams.get(i); - if (isArrayOrList(paramType, param)) { // Generate loop - println("for (" + getGenericType(paramType) + "* cb : " + getSimpleIdentifier(param) + ") {"); - println(callbackType + "_CallbackSkeleton* skel = new " + callbackType + "_CallbackSkeleton(cb, objIdCnt++);"); - isArrayOrList = true; - callbackParam = getSimpleIdentifier(param); - } else - println(callbackType + "_CallbackSkeleton* skel = new " + callbackType + "_CallbackSkeleton(" + - getSimpleIdentifier(param) + ", objIdCnt++);"); - println("vecCallbackObj.push_back(skel);"); + print("int ___paramCB" + i + " = "); if (isArrayOrList) - println("}"); - } - } - println("int numParam = " + methParams.size() + ";"); - println("int methodId = " + intDecl.getMethodNumId(method) + ";"); - String retType = intDecl.getMethodType(method); - //String retTypeC = checkAndGetCplusType(retType); - //println("string retType = \"" + checkAndGetCplusArrayType(getStructType(getEnumType(retTypeC))) + "\";"); - println("string retType = \"" + checkAndGetCplusRetClsType(getStructType(getEnumType(retType))) + "\";"); - // Generate array of parameter types - print("string paramCls[] = { "); - for (int i = 0; i < methParams.size(); i++) { - String paramType = methPrmTypes.get(i); - if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object - print("\"int\""); - } else { // Generate normal classes if it's not a callback object - String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i)); - print("\"" + paramTypeC + "\""); + println(callbackParam + ".size();"); + else + println("1;"); } - if (i != methParams.size() - 1) // Check if this is the last element - print(", "); } - println(" };"); - print("int ___paramCB = "); - if (isArrayOrList) - println(callbackParam + ".size();"); - else - println("1;"); - // Generate array of parameter objects - print("void* paramObj[] = { "); - for (int i = 0; i < methParams.size(); i++) { - String paramType = methPrmTypes.get(i); - if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object - print("&___paramCB"); - } else - print("&" + getSimpleIdentifier(methParams.get(i))); - if (i != methParams.size() - 1) - print(", "); - } - println(" };"); - // Check if this is "void" - if (retType.equals("void")) { - println("void* retObj = NULL;"); - println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);"); - } else { // We do have a return value - if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES) - println(checkAndGetCplusType(retType) + " retVal;"); - else - println(checkAndGetCplusType(retType) + " retVal = " + generateCplusInitializer(retType) + ";"); - println("void* retObj = &retVal;"); - println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);"); - println("return retVal;"); - } - }*/ + } /** @@ -3017,7 +2890,7 @@ public class IoTCompiler { writeStructMembersCplusStub(simpleType, paramType, param); } else if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object println("paramCls[pos] = \"int\";"); - println("paramObj[pos++] = &___paramCB;"); + println("paramObj[pos++] = &___paramCB" + i + ";"); } else { String prmTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i)); println("paramCls[pos] = \"" + prmTypeC + "\";"); @@ -3153,7 +3026,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = returnGenericCallbackType(methPrmTypes.get(i)); if (checkCallbackType(paramType, callbackType)) // Check if this has callback object - print("&___paramCB"); + print("&___paramCB" + i); else print("&" + getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i)); // Check if this is the last element (don't print a comma) @@ -4000,13 +3873,13 @@ public class IoTCompiler { for (int i = 0; i < members.size(); i++) { String prmTypeC = checkAndGetCplusType(memTypes.get(i)); String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i)); - println(getSimpleType(getEnumType(prmType)) + " param" + i + "[" + counter + "];"); + println(getSimpleType(getEnumType(prmType)) + " param" + iVar + i + "[" + counter + "];"); } } else { // Just one struct element for (int i = 0; i < members.size(); i++) { String prmTypeC = checkAndGetCplusType(memTypes.get(i)); String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i)); - println(getSimpleType(getEnumType(prmType)) + " param" + i + ";"); + println(getSimpleType(getEnumType(prmType)) + " param" + iVar + i + ";"); } } if (isArrayOrList(paramType, param)) { // An array or list @@ -4016,14 +3889,14 @@ public class IoTCompiler { for (int i = 0; i < members.size(); i++) { String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i)); println("paramCls[pos] = \"" + prmTypeC + "\";"); - println("paramObj[pos++] = ¶m" + i + "[i];"); + println("paramObj[pos++] = ¶m" + iVar + i + "[i];"); } println("}"); } else { // Just one struct element for (int i = 0; i < members.size(); i++) { String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i)); println("paramCls[pos] = \"" + prmTypeC + "\";"); - println("paramObj[pos++] = ¶m" + i + ";"); + println("paramObj[pos++] = ¶m" + iVar + i + ";"); } } } @@ -4055,13 +3928,13 @@ public class IoTCompiler { println("for(int i = 0; i < " + counter + "; i++) {"); for (int j = 0; j < members.size(); j++) { print("paramStruct" + i + "[i]." + getSimpleIdentifier(members.get(j))); - println(" = param" + j + "[i];"); + println(" = param" + i + j + "[i];"); } println("}"); } else { // Just one struct element for (int j = 0; j < members.size(); j++) { print("paramStruct" + i + "." + getSimpleIdentifier(members.get(j))); - println(" = param" + j + ";"); + println(" = param" + i + j + ";"); } } } @@ -4193,9 +4066,10 @@ public class IoTCompiler { String param = methParams.get(i); String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { - if (!begin) { // Generate comma for not the beginning variable - print(", "); begin = false; - } + if (!begin) // Generate comma for not the beginning variable + print(", "); + else + begin = false; int methodNumId = intDecl.getMethodNumId(method); print("int struct" + methodNumId + "Size" + i); } @@ -4508,9 +4382,10 @@ public class IoTCompiler { String param = methParams.get(i); String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { - if (!begin) { // Generate comma for not the beginning variable - print(", "); begin = false; - } + if (!begin) // Generate comma for not the beginning variable + print(", "); + else + begin = false; int methodNumId = intDecl.getMethodNumId(method); print("int struct" + methodNumId + "Size" + i); }