ReturnInformation.java 5.6 KB

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