ReturnInformation.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data;
  2. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.exceptions.InvalidConversionException;
  3. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.generation.Conversions;
  4. public class ReturnInformation {
  5. private SVType type;
  6. private boolean typeIsSet;
  7. private Object value;
  8. private String code = "";
  9. private SAScalarVariable ConSaSv;
  10. private GlobalInOutVariable conGlobVar;
  11. private boolean forceType = false;
  12. public ReturnInformation() {
  13. // TODO Auto-generated constructor stub
  14. }
  15. public void appendCode(String code) {
  16. this.code += code;
  17. }
  18. public String getCode() {
  19. return code;
  20. }
  21. public void setCode(String code) {
  22. this.code = code;
  23. }
  24. public SVType getType() throws Exception {
  25. if(!typeIsSet)
  26. {
  27. throw new Exception(
  28. "Attempt to retrieve unset type for code: " + code);
  29. }
  30. return type;
  31. }
  32. public void setType(SVType type) throws InvalidConversionException {
  33. if (this.typeIsSet) {
  34. this.type = Conversions.typeDecider(this.type, type);
  35. } else {
  36. this.type = type;
  37. this.typeIsSet = true;
  38. }
  39. }
  40. public Object getValue() {
  41. return value;
  42. }
  43. public void setValue(Object value) {
  44. this.value = value;
  45. }
  46. public boolean isTypeIsSet() {
  47. return typeIsSet;
  48. }
  49. public SAScalarVariable getConSaSv() {
  50. return ConSaSv;
  51. }
  52. public void setConSaSv(SAScalarVariable conSaSv) {
  53. ConSaSv = conSaSv;
  54. }
  55. public GlobalInOutVariable getConGlobVar() {
  56. return conGlobVar;
  57. }
  58. public void setConGlobVar(GlobalInOutVariable conGlobVar) throws InvalidConversionException {
  59. this.conGlobVar = conGlobVar;
  60. if (this.typeIsSet) {
  61. this.type = Conversions.typeDecider(conGlobVar.type, this.type);
  62. }
  63. else
  64. {
  65. this.type = conGlobVar.type;
  66. this.typeIsSet = true;
  67. }
  68. }
  69. /*
  70. * This method automatically extracts type information
  71. */
  72. public ReturnInformation(ReturnInformation information) throws InvalidConversionException {
  73. this.conGlobVar = information.conGlobVar;
  74. if (information.typeIsSet)
  75. this.setType(information.type);
  76. this.ConSaSv = information.ConSaSv;
  77. }
  78. /*
  79. * This method automatically extracts and compares type information
  80. */
  81. public ReturnInformation(ReturnInformation information, ReturnInformation information2) throws Exception {
  82. if (information.conGlobVar != null) {
  83. if (information2.conGlobVar != null) {
  84. if (information.conGlobVar == information2.conGlobVar) {
  85. this.conGlobVar = information.conGlobVar;
  86. }
  87. // In this case they must have the same type otherwise the
  88. // return value is impossible to typecheck.
  89. else if (information.conGlobVar.type != information2.conGlobVar.type) {
  90. throw new Exception("The two connected global variables: " + information.conGlobVar.name
  91. + " and " + information2.conGlobVar.name + " have different types");
  92. }
  93. } else {
  94. this.conGlobVar = information.conGlobVar;
  95. }
  96. } else {
  97. if (information2.conGlobVar != null) {
  98. this.conGlobVar = information2.conGlobVar;
  99. }
  100. }
  101. if (information.typeIsSet) {
  102. if (information2.typeIsSet == false) {
  103. this.setType(information.getType());
  104. } else {
  105. if (information.getType() != information2.getType()) {
  106. this.type = Conversions.typeDecider(information.getType(), information2.getType());
  107. } else {
  108. this.setType(information.getType());
  109. }
  110. }
  111. } else {
  112. if (information2.typeIsSet) {
  113. this.setType(information2.getType());
  114. }
  115. }
  116. if (information.ConSaSv != null) {
  117. if (information2.ConSaSv != null) {
  118. if (information.ConSaSv != information2.ConSaSv) {
  119. throw new Exception(
  120. "Two connected return informations contain different ConSaSv have the wrong type: "
  121. + information.ConSaSv.getName() + " and " + information2.ConSaSv.getName());
  122. } else {
  123. this.setConSaSv(information.ConSaSv);
  124. }
  125. } else {
  126. this.setConSaSv(information.ConSaSv);
  127. }
  128. } else {
  129. if (information2.ConSaSv != null) {
  130. this.setConSaSv(information2.ConSaSv);
  131. }
  132. }
  133. if(information.forceType && information2.forceType)
  134. {
  135. if(information.getType() != information2.getType())
  136. {
  137. throw new Exception(
  138. "Two connected return informations with force types contain different types: "
  139. + information.getType() + " and " + information2.getType());
  140. }
  141. }
  142. else if(information.forceType || information2.forceType)
  143. {
  144. this.forceType = true;
  145. if(information.forceType)
  146. this.type = information.type;
  147. else
  148. this.type = information2.type;
  149. }
  150. }
  151. public boolean getForceType() {
  152. return forceType;
  153. }
  154. public void setForceType(boolean forceType) {
  155. this.forceType = forceType;
  156. }
  157. }