/projects/highLevelGrammars/AbstractionAttribute.C

https://bitbucket.org/bathtub/rose · C · 373 lines · 231 code · 88 blank · 54 comment · 65 complexity · 84523f9b399025afb7e572cadedeceb1 MD5 · raw file

  1. // ROSE is a tool for building preprocessors, this file is an example preprocessor built with ROSE.
  2. // rose.C: Example (default) ROSE Preprocessor: used for testing ROSE infrastructure
  3. // #include <string>
  4. // #include <iomanip>
  5. #include "rose.h"
  6. // #include "AstTests.h"
  7. #include <algorithm>
  8. // DQ (1/1/2006): This is OK if not declared in a header file
  9. using namespace std;
  10. #include "nodeAndEdgeTypes.h"
  11. // Support for overloaded operators
  12. #include "AbstractionAttribute.h"
  13. SgExpression*
  14. ExpressionStatementAttribute::getExpression ( SgExprStatement* expressionStatement )
  15. {
  16. SgExpression* returnExpression = expressionStatement->get_expression();
  17. ROSE_ASSERT(returnExpression != NULL);
  18. return returnExpression;
  19. }
  20. list<SgNode*>
  21. ExpressionStatementAttribute::getOperatorNodes ( SgExprStatement* expressionStatement )
  22. {
  23. // build list of SgNodes associated with this operator
  24. list<SgNode*> operatorNodeList;
  25. operatorNodeList.push_back(expressionStatement);
  26. // DQ (11/8/2006): The SgExpressionRoot is not longer used.
  27. // Include the expression root in the operator definition
  28. // operatorNodeList.push_back(expressionStatement->get_expression());
  29. SgExpression* returnExpression = expressionStatement->get_expression();
  30. if (isSgInitializer(returnExpression) != NULL)
  31. {
  32. operatorNodeList.push_back(returnExpression);
  33. }
  34. return operatorNodeList;
  35. }
  36. SgExpression*
  37. ExpressionAttribute::chaseFunctionCall ( SgExpression* exp )
  38. {
  39. // SgInitializer* initializer = isSgInitializer(returnExpression);
  40. SgExpression* returnExpression = exp;
  41. // look a little deeper for the next SgFunction
  42. switch(exp->variantT())
  43. {
  44. case V_SgAssignInitializer:
  45. {
  46. SgAssignInitializer* assignmentInitializer = isSgAssignInitializer(exp);
  47. returnExpression = assignmentInitializer->get_operand();
  48. break;
  49. }
  50. default:
  51. printf ("Error: default reached \n");
  52. }
  53. ROSE_ASSERT (returnExpression != NULL);
  54. return returnExpression;
  55. }
  56. list<SgNode*>
  57. BinaryOperatorAttribute::getOperatorNodes(SgFunctionCallExp* functionCallExpression)
  58. {
  59. // NodeType graphNode(fc,"color=red ");
  60. // listOfNodes.push_back(graphNode);
  61. list<SgNode*> operatorNodeList;
  62. ROSE_ASSERT (functionCallExpression != NULL);
  63. operatorNodeList.push_back(functionCallExpression);
  64. ROSE_ASSERT (functionCallExpression->get_function() != NULL);
  65. operatorNodeList.push_back(functionCallExpression->get_function());
  66. // Get the number of parameters to this function
  67. SgExprListExp* exprListExp = functionCallExpression->get_args();
  68. ROSE_ASSERT (exprListExp != NULL);
  69. operatorNodeList.push_back(exprListExp);
  70. SgExpressionPtrList & expressionPtrList = exprListExp->get_expressions();
  71. int numberOfParameters = expressionPtrList.size();
  72. SgExpression* returnExpression = NULL;
  73. // Member function binary operators take only 1 parameter while global
  74. // (most often friend) function binary operators take 2 paramters.
  75. if (numberOfParameters == 1)
  76. {
  77. // This this should be a member function of a class (chek it)
  78. SgDotExp* dotExpression = isSgDotExp(functionCallExpression->get_function());
  79. ROSE_ASSERT (dotExpression != NULL);
  80. returnExpression = dotExpression->get_lhs_operand();
  81. operatorNodeList.push_back(dotExpression);
  82. if (isSgInitializer(returnExpression) != NULL)
  83. {
  84. operatorNodeList.push_back(returnExpression);
  85. returnExpression = chaseFunctionCall(returnExpression);
  86. }
  87. operatorNodeList.push_back(dotExpression->get_rhs_operand());
  88. // rhs is the first (only) parameter
  89. returnExpression = *(expressionPtrList.begin());
  90. if (isSgInitializer(returnExpression))
  91. {
  92. operatorNodeList.push_back(returnExpression);
  93. returnExpression = chaseFunctionCall(returnExpression);
  94. }
  95. }
  96. else
  97. {
  98. ROSE_ASSERT (numberOfParameters == 2);
  99. // lhs is the first parameter
  100. returnExpression = *(expressionPtrList.begin());
  101. if (isSgInitializer(returnExpression) != NULL)
  102. {
  103. operatorNodeList.push_back(returnExpression);
  104. returnExpression = chaseFunctionCall(returnExpression);
  105. }
  106. // rhs is the last (2nd) parameter
  107. returnExpression = *(expressionPtrList.rbegin());
  108. if (isSgInitializer(returnExpression) != NULL)
  109. {
  110. operatorNodeList.push_back(returnExpression);
  111. returnExpression = chaseFunctionCall(returnExpression);
  112. }
  113. }
  114. ROSE_ASSERT (operatorNodeList.size() > 0);
  115. return operatorNodeList;
  116. }
  117. SgExpression*
  118. BinaryOperatorAttribute::getLhsOperand ( SgFunctionCallExp* functionCallExpression )
  119. {
  120. /*
  121. A function call (SgFunctionCallExp) contains:
  122. function:
  123. either a function reference expression (SgFunctionRefExp)
  124. OR
  125. a dotExp (containing member function reference expression (SgMemberFunctionRefExp)
  126. function arguments:
  127. an expression list (SgExpreListExp)
  128. From the input function call (for an overloader operator)
  129. generate a pointer to the lhs operand.
  130. */
  131. ROSE_ASSERT (functionCallExpression != NULL);
  132. // Get the number of parameters to this function
  133. SgExprListExp* exprListExp = functionCallExpression->get_args();
  134. ROSE_ASSERT (exprListExp != NULL);
  135. SgExpressionPtrList & expressionPtrList = exprListExp->get_expressions();
  136. int numberOfParameters = expressionPtrList.size();
  137. SgExpression* returnExpression = NULL;
  138. // Member function binary operators take only 1 parameter while global
  139. // (most often friend) function binary operators take 2 paramters.
  140. if (numberOfParameters == 1)
  141. {
  142. // This should be a member function of a class (chek it)
  143. SgDotExp* dotExpression = isSgDotExp(functionCallExpression->get_function());
  144. ROSE_ASSERT (dotExpression != NULL);
  145. returnExpression = dotExpression->get_lhs_operand();
  146. if (isSgInitializer(returnExpression) != NULL)
  147. returnExpression = chaseFunctionCall(returnExpression);
  148. }
  149. else
  150. {
  151. ROSE_ASSERT (numberOfParameters == 2);
  152. // lhs is the first parameter
  153. returnExpression = *(expressionPtrList.begin());
  154. if (isSgInitializer(returnExpression) != NULL)
  155. returnExpression = chaseFunctionCall(returnExpression);
  156. #if 0
  157. SgInitializer* initializer = isSgInitializer(returnExpression);
  158. if (initializer != NULL)
  159. {
  160. // look a little deeper for the next SgFunction
  161. switch(initializer->variantT())
  162. {
  163. case V_SgAssignInitializer:
  164. {
  165. SgAssignInitializer* assignmentInitializer = isSgAssignInitializer(initializer);
  166. returnExpression = assignmentInitializer->get_operand();
  167. break;
  168. }
  169. default:
  170. printf ("Error: default reached \n");
  171. }
  172. ROSE_ASSERT (returnExpression != NULL);
  173. }
  174. #endif
  175. }
  176. ROSE_ASSERT (returnExpression != NULL);
  177. return returnExpression;
  178. }
  179. SgExpression*
  180. BinaryOperatorAttribute::getRhsOperand ( SgFunctionCallExp* functionCallExpression )
  181. {
  182. ROSE_ASSERT (functionCallExpression != NULL);
  183. // Get the number of parameters to this function
  184. SgExprListExp* exprListExp = functionCallExpression->get_args();
  185. ROSE_ASSERT (exprListExp != NULL);
  186. SgExpressionPtrList & expressionPtrList = exprListExp->get_expressions();
  187. int numberOfParameters = expressionPtrList.size();
  188. SgExpression* returnExpression = NULL;
  189. // Member function binary operators take only 1 parameter while global
  190. // (most often friend) function binary operators take 2 paramters.
  191. if (numberOfParameters == 1)
  192. {
  193. // rhs is the first (only) parameter
  194. returnExpression = *(expressionPtrList.begin());
  195. if (isSgInitializer(returnExpression))
  196. returnExpression = chaseFunctionCall(returnExpression);
  197. }
  198. else
  199. {
  200. ROSE_ASSERT (numberOfParameters == 2);
  201. // lhs is the last parameter
  202. returnExpression = *(expressionPtrList.rbegin());
  203. if (isSgInitializer(returnExpression))
  204. returnExpression = chaseFunctionCall(returnExpression);
  205. }
  206. ROSE_ASSERT (returnExpression != NULL);
  207. return returnExpression;
  208. }
  209. int
  210. ParenthesisOperatorAttribute::getNumberOfIndexOperands(SgFunctionCallExp* functionCallExpression)
  211. {
  212. // get number of operands
  213. ROSE_ASSERT (functionCallExpression != NULL);
  214. // Get the number of parameters to this function
  215. SgExprListExp* exprListExp = functionCallExpression->get_args();
  216. ROSE_ASSERT (exprListExp != NULL);
  217. SgExpressionPtrList & expressionPtrList = exprListExp->get_expressions();
  218. int numberOfParameters = expressionPtrList.size();
  219. return numberOfParameters;
  220. }
  221. SgExpression*
  222. ParenthesisOperatorAttribute::getIndex ( SgFunctionCallExp* functionCallExpression, unsigned int n )
  223. {
  224. // get the nth operand from the index list
  225. ROSE_ASSERT (functionCallExpression != NULL);
  226. // Get the number of parameters to this function
  227. SgExprListExp* exprListExp = functionCallExpression->get_args();
  228. ROSE_ASSERT (exprListExp != NULL);
  229. SgExpressionPtrList & expressionPtrList = exprListExp->get_expressions();
  230. // int numberOfParameters = expressionPtrList.size();
  231. unsigned int counter = 0;
  232. SgExpression* returnExpression = NULL;
  233. for (SgExpressionPtrList::iterator i = expressionPtrList.begin(); i != expressionPtrList.end(); i++)
  234. {
  235. if (counter++ == n)
  236. returnExpression = *i;
  237. }
  238. ROSE_ASSERT (counter <= expressionPtrList.size());
  239. if (isSgInitializer(returnExpression) != NULL)
  240. returnExpression = chaseFunctionCall(returnExpression);
  241. ROSE_ASSERT (returnExpression != NULL);
  242. return returnExpression;
  243. }
  244. SgExpression*
  245. ParenthesisOperatorAttribute::getIndexedOperand(SgFunctionCallExp* functionCallExpression)
  246. {
  247. // get the indexed operand
  248. SgExpression* returnExpression = NULL;
  249. // This should be a member function of a class (chek it)
  250. SgDotExp* dotExpression = isSgDotExp(functionCallExpression->get_function());
  251. ROSE_ASSERT (dotExpression != NULL);
  252. returnExpression = dotExpression->get_lhs_operand();
  253. if (isSgInitializer(returnExpression) != NULL)
  254. returnExpression = chaseFunctionCall(returnExpression);
  255. ROSE_ASSERT (returnExpression != NULL);
  256. return returnExpression;
  257. }
  258. list<SgNode*>
  259. ParenthesisOperatorAttribute::getOperatorNodes(SgFunctionCallExp* functionCallExpression)
  260. {
  261. // build list of SgNodes associated with this operator
  262. list<SgNode*> operatorNodeList;
  263. SgExprListExp* exprListExp = functionCallExpression->get_args();
  264. ROSE_ASSERT (exprListExp != NULL);
  265. operatorNodeList.push_back(exprListExp);
  266. SgExpressionPtrList & expressionPtrList = exprListExp->get_expressions();
  267. for (SgExpressionPtrList::iterator i = expressionPtrList.begin(); i != expressionPtrList.end(); i++)
  268. {
  269. if (isSgInitializer(*i) != NULL)
  270. operatorNodeList.push_back(*i);
  271. }
  272. // This should be a member function of a class (chek it)
  273. SgDotExp* dotExpression = isSgDotExp(functionCallExpression->get_function());
  274. ROSE_ASSERT (dotExpression != NULL);
  275. operatorNodeList.push_back(dotExpression);
  276. operatorNodeList.push_back(dotExpression->get_rhs_operand());
  277. SgExpression* returnExpression = dotExpression->get_rhs_operand();
  278. if (isSgInitializer(returnExpression) != NULL)
  279. {
  280. operatorNodeList.push_back(returnExpression);
  281. returnExpression = chaseFunctionCall(returnExpression);
  282. }
  283. return operatorNodeList;
  284. }