You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProceedVisitor.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.internal.compiler.ast;
  13. import org.aspectj.weaver.Advice;
  14. import org.aspectj.org.eclipse.jdt.internal.compiler.ASTVisitor;
  15. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
  16. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Expression;
  17. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.MessageSend;
  18. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.NameReference;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
  20. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.Binding;
  21. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BlockScope;
  22. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ClassScope;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
  25. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
  26. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
  27. /**
  28. * Takes a method that already has the three extra parameters
  29. * thisJoinPointStaticPart, thisJoinPoint and thisEnclosingJoinPointStaticPart
  30. */
  31. public class ProceedVisitor extends ASTVisitor {
  32. boolean needsDynamic = false;
  33. boolean needsStatic = false;
  34. boolean needsStaticEnclosing = false;
  35. boolean hasEffectivelyStaticRef = false;
  36. LocalVariableBinding thisJoinPointDec;
  37. LocalVariableBinding thisJoinPointStaticPartDec;
  38. LocalVariableBinding thisEnclosingJoinPointStaticPartDec;
  39. LocalVariableBinding thisJoinPointDecLocal;
  40. LocalVariableBinding thisJoinPointStaticPartDecLocal;
  41. LocalVariableBinding thisEnclosingJoinPointStaticPartDecLocal;
  42. boolean replaceEffectivelyStaticRefs = false;
  43. AbstractMethodDeclaration method;
  44. ProceedVisitor(AbstractMethodDeclaration method) {
  45. this.method = method;
  46. int index = method.arguments.length - 3;
  47. thisJoinPointStaticPartDecLocal = method.scope.locals[index];
  48. thisJoinPointStaticPartDec = method.arguments[index++].binding;
  49. thisJoinPointDecLocal = method.scope.locals[index];
  50. thisJoinPointDec = method.arguments[index++].binding;
  51. thisEnclosingJoinPointStaticPartDecLocal = method.scope.locals[index];
  52. thisEnclosingJoinPointStaticPartDec = method.arguments[index++].binding;
  53. }
  54. public void computeJoinPointParams() {
  55. // walk my body to see what is needed
  56. method.traverse(this, (ClassScope) null);
  57. //??? add support for option to disable this optimization
  58. //System.err.println("check: "+ hasEffectivelyStaticRef + ", " + needsDynamic);
  59. if (hasEffectivelyStaticRef && !needsDynamic) {
  60. // replace effectively static refs with thisJoinPointStaticPart
  61. replaceEffectivelyStaticRefs = true;
  62. needsStatic = true;
  63. method.traverse(this, (ClassScope) null);
  64. }
  65. }
  66. boolean isRef(NameReference ref, Binding binding) {
  67. return ref.binding == binding;
  68. }
  69. boolean isRef(Expression expr, Binding binding) {
  70. //System.err.println("isRef: " + expr + ", " + binding);
  71. return expr != null
  72. && expr instanceof NameReference
  73. && isRef((NameReference) expr, binding);
  74. }
  75. public void endVisit(SingleNameReference ref, BlockScope scope) {
  76. if (isRef(ref, thisJoinPointDec))
  77. needsDynamic = true;
  78. else if (isRef(ref, thisJoinPointStaticPartDec))
  79. needsStatic = true;
  80. else if (isRef(ref, thisEnclosingJoinPointStaticPartDec))
  81. needsStaticEnclosing = true;
  82. }
  83. // public void checkAndFix(ASTObject body) {
  84. // this.process(body);
  85. // if (needsFakeStatic && !needsDynamic) {
  86. // if (!this.getCompiler().getOptions().noMetaJoinPointOptimization) {
  87. // makeFakeStatics = true;
  88. // needsStatic = true;
  89. // this.process(body);
  90. // } else {
  91. // needsDynamic = true;
  92. // }
  93. // }
  94. // }
  95. boolean canTreatAsStatic(String id) {
  96. return id.equals("toString")
  97. || id.equals("toShortString")
  98. || id.equals("toLongString")
  99. || id.equals("getKind")
  100. || id.equals("getSignature")
  101. || id.equals("getSourceLocation")
  102. || id.equals("getStaticPart");
  103. }
  104. // boolean canTreatAsStatic(VarExpr varExpr) {
  105. // ASTObject parent = varExpr.getParent();
  106. // if (parent instanceof CallExpr) {
  107. // Method calledMethod = ((CallExpr)parent).getMethod();
  108. // return canTreatAsStatic(calledMethod);
  109. //
  110. // //??? should add a case here to catch
  111. // //??? tjp.getEnclosingExecutionJoinPoint().STATIC_METHOD()
  112. // } else if (parent instanceof BinopExpr) {
  113. // BinopExpr binop = (BinopExpr)parent;
  114. // if (binop.getType().isEquivalent(this.getTypeManager().getStringType())) {
  115. // return true;
  116. // } else {
  117. // return false;
  118. // }
  119. // } else {
  120. // return false;
  121. // }
  122. // }
  123. boolean inBlockThatCantRun = false;
  124. public boolean visit(MessageSend call, BlockScope scope) {
  125. Expression receiver = call.receiver;
  126. if (isRef(receiver, thisJoinPointDec)) {
  127. if (canTreatAsStatic(new String(call.selector))) {
  128. if (replaceEffectivelyStaticRefs) {
  129. replaceEffectivelyStaticRef(call);
  130. } else {
  131. //System.err.println("has static reg");
  132. hasEffectivelyStaticRef = true;
  133. if (call.arguments != null) {
  134. int argumentsLength = call.arguments.length;
  135. for (int i = 0; i < argumentsLength; i++)
  136. call.arguments[i].traverse(this, scope);
  137. }
  138. return false;
  139. }
  140. }
  141. }
  142. return super.visit(call, scope);
  143. }
  144. private void replaceEffectivelyStaticRef(MessageSend call) {
  145. //System.err.println("replace static ref");
  146. NameReference receiver = (NameReference) call.receiver;
  147. receiver.binding = thisJoinPointStaticPartDecLocal; //thisJoinPointStaticPartDec;
  148. // receiver.codegenBinding = thisJoinPointStaticPartDecLocal;
  149. call.binding.declaringClass =
  150. (ReferenceBinding) thisJoinPointStaticPartDec.type;
  151. }
  152. public int removeUnusedExtraArguments() {
  153. int extraArgumentFlags = 0;
  154. this.computeJoinPointParams();
  155. MethodBinding binding = method.binding;
  156. int index = binding.parameters.length - 3;
  157. if (needsStaticEnclosing) {
  158. extraArgumentFlags |= Advice.ThisEnclosingJoinPointStaticPart;
  159. } else {
  160. removeParameter(index+2);
  161. }
  162. if (needsDynamic) {
  163. extraArgumentFlags |= Advice.ThisJoinPoint;
  164. } else {
  165. removeParameter(index+1);
  166. }
  167. if (needsStatic) {
  168. extraArgumentFlags |= Advice.ThisJoinPointStaticPart;
  169. } else {
  170. removeParameter(index+0);
  171. }
  172. return extraArgumentFlags;
  173. }
  174. private void removeParameter(int indexToRemove) {
  175. // TypeBinding[] parameters = method.binding.parameters;
  176. method.scope.locals = removeLocalBinding(indexToRemove, method.scope.locals);
  177. method.binding.parameters = removeParameter(indexToRemove, method.binding.parameters);
  178. }
  179. private static TypeBinding[] removeParameter(int index, TypeBinding[] bindings) {
  180. int len = bindings.length;
  181. TypeBinding[] ret = new TypeBinding[len-1];
  182. System.arraycopy(bindings, 0, ret, 0, index);
  183. System.arraycopy(bindings, index+1, ret, index, len-index-1);
  184. return ret;
  185. }
  186. private static LocalVariableBinding[] removeLocalBinding(int index, LocalVariableBinding[] bindings) {
  187. int len = bindings.length;
  188. //??? for performance we should do this in-place
  189. LocalVariableBinding[] ret = new LocalVariableBinding[len-1];
  190. System.arraycopy(bindings, 0, ret, 0, index);
  191. System.arraycopy(bindings, index+1, ret, index, len-index-1);
  192. return ret;
  193. }
  194. }