Posted by Andrey Tarantsov Sat 24th Feb 2007 19:56 - Syntax is Diff - 55 views
Download | New Post | Modify | Hide line numbers
  1. ### Eclipse Workspace Patch 1.0
  2. #P org.eclipse.dltk.core
  3. Index: typeinference/org/eclipse/dltk/ddp/BasicContext.java
  4. ===================================================================
  5. RCS file: /cvsroot/technology/org.eclipse.dltk/core/plugins/org.eclipse.dltk.core/typeinference/org/eclipse/dltk/ddp/BasicContext.java,v
  6. retrieving revision 1.1
  7. diff -u -r1.1 BasicContext.java
  8. --- typeinference/org/eclipse/dltk/ddp/BasicContext.java    23 Feb 2007 14:22:15 -0000    1.1
  9. +++ typeinference/org/eclipse/dltk/ddp/BasicContext.java    24 Feb 2007 19:52:24 -0000
  10. @@ -13,6 +13,11 @@
  11.          this.rootNode = rootNode;
  12.      }
  13.  
  14. +    public BasicContext(BasicContext parent) {
  15. +        sourceModule = parent.sourceModule;
  16. +        rootNode = parent.rootNode;
  17. +    }
  18. +
  19.      public ModuleDeclaration getRootNode() {
  20.          return rootNode;
  21.      }
  22. #P org.eclipse.dltk.ruby.core.tests
  23. Index: workspace/typeinference/variables/self_singletons.rb
  24. ===================================================================
  25. RCS file: workspace/typeinference/variables/self_singletons.rb
  26. diff -N workspace/typeinference/variables/self_singletons.rb
  27. --- /dev/null    1 Jan 1970 00:00:00 -0000
  28. +++ workspace/typeinference/variables/self_singletons.rb    1 Jan 1970 00:00:00 -0000
  29. @@ -0,0 +1,18 @@
  30. +
  31. +class BozSelfSingletons
  32. +    class Bar
  33. +        self ## expr self => BozSelfSingletons::Bar
  34. +       
  35. +        def foo
  36. +            self ## expr self => BozSelfSingletons::Bar.new
  37. +        end
  38. +       
  39. +        def self.foo
  40. +            self ## expr self => BozSelfSingletons::Bar
  41. +        end
  42. +       
  43. +        def Bar.foo
  44. +            self ## expr self => BozSelfSingletons::Bar
  45. +        end
  46. +    end
  47. +end
  48. #P org.eclipse.dltk.ruby.core
  49. Index: src/org/eclipse/dltk/ruby/typeinference/RubyTypeInferencingUtils.java
  50. ===================================================================
  51. RCS file: /cvsroot/technology/org.eclipse.dltk/ruby/plugins/org.eclipse.dltk.ruby.core/src/org/eclipse/dltk/ruby/typeinference/RubyTypeInferencingUtils.java,v
  52. retrieving revision 1.3
  53. diff -u -r1.3 RubyTypeInferencingUtils.java
  54. --- src/org/eclipse/dltk/ruby/typeinference/RubyTypeInferencingUtils.java    24 Feb 2007 18:56:59 -0000    1.3
  55. +++ src/org/eclipse/dltk/ruby/typeinference/RubyTypeInferencingUtils.java    24 Feb 2007 19:52:27 -0000
  56. @@ -32,6 +32,8 @@
  57.  import org.eclipse.dltk.core.search.SearchEngine;
  58.  import org.eclipse.dltk.core.search.SearchParticipant;
  59.  import org.eclipse.dltk.core.search.SearchPattern;
  60. +import org.eclipse.dltk.ddp.BasicContext;
  61. +import org.eclipse.dltk.ddp.IContext;
  62.  import org.eclipse.dltk.evaluation.types.AmbiguousType;
  63.  import org.eclipse.dltk.evaluation.types.IClassType;
  64.  import org.eclipse.dltk.evaluation.types.IEvaluatedType;
  65. @@ -265,6 +267,17 @@
  66.          return null;
  67.      }
  68.  
  69. +    public static IEvaluatedType determineSelfClass(IContext context, int keyOffset) {
  70. +        if (context instanceof InstanceContext) {
  71. +            InstanceContext instanceContext = (InstanceContext) context;
  72. +            return instanceContext.getInstanceType();
  73. +        } else {
  74. +            BasicContext basicContext = (BasicContext) context;
  75. +            return determineSelfClass(basicContext.getSourceModule(), basicContext.getRootNode(),
  76. +                    keyOffset);
  77. +        }
  78. +    }
  79. +
  80.      /**
  81.       * Determines a fully-qualified names of the class scope that the given
  82.       * offset is statically enclosed in.
  83. Index: src/org/eclipse/dltk/ruby/typeinference/InstanceContext.java
  84. ===================================================================
  85. RCS file: /cvsroot/technology/org.eclipse.dltk/ruby/plugins/org.eclipse.dltk.ruby.core/src/org/eclipse/dltk/ruby/typeinference/InstanceContext.java,v
  86. retrieving revision 1.1
  87. diff -u -r1.1 InstanceContext.java
  88. --- src/org/eclipse/dltk/ruby/typeinference/InstanceContext.java    24 Feb 2007 18:56:59 -0000    1.1
  89. +++ src/org/eclipse/dltk/ruby/typeinference/InstanceContext.java    24 Feb 2007 19:52:27 -0000
  90. @@ -13,6 +13,11 @@
  91.          super(sourceModule, rootNode);
  92.          this.instanceType = instanceType;
  93.      }
  94. +   
  95. +    public InstanceContext(BasicContext parent, IEvaluatedType instanceType) {
  96. +        super(parent);
  97. +        this.instanceType = instanceType;
  98. +    }
  99.  
  100.      public IEvaluatedType getInstanceType() {
  101.          return instanceType;
  102. Index: src/org/eclipse/dltk/ruby/typeinference/RubyEvaluatorFactory.java
  103. ===================================================================
  104. RCS file: /cvsroot/technology/org.eclipse.dltk/ruby/plugins/org.eclipse.dltk.ruby.core/src/org/eclipse/dltk/ruby/typeinference/RubyEvaluatorFactory.java,v
  105. retrieving revision 1.3
  106. diff -u -r1.3 RubyEvaluatorFactory.java
  107. --- src/org/eclipse/dltk/ruby/typeinference/RubyEvaluatorFactory.java    24 Feb 2007 18:56:59 -0000    1.3
  108. +++ src/org/eclipse/dltk/ruby/typeinference/RubyEvaluatorFactory.java    24 Feb 2007 19:52:27 -0000
  109. @@ -1,6 +1,7 @@
  110.  package org.eclipse.dltk.ruby.typeinference;
  111.  
  112.  import org.eclipse.dltk.ast.expressions.Assignment;
  113. +import org.eclipse.dltk.ast.expressions.CallExpression;
  114.  import org.eclipse.dltk.ast.expressions.NumericLiteral;
  115.  import org.eclipse.dltk.ast.expressions.StringLiteral;
  116.  import org.eclipse.dltk.ast.references.ConstantReference;
  117. @@ -32,8 +33,12 @@
  118.                  return new AssignmentEvaluator(goal);
  119.              else if (expr instanceof SelfReference)
  120.                  return new SelfReferenceEvaluator(goal);
  121. +            else if (expr instanceof CallExpression)
  122. +                return new MethodCallTypeEvaluator((ExpressionGoal) goal);
  123.          } else if (goal instanceof ConstantTypeGoal)
  124.              return new ConstantReferenceEvaluator((ConstantTypeGoal) goal);
  125. +        else if (goal instanceof MethodReturnTypeGoal)
  126. +            return new StringLiteralEvaluator(goal);
  127.          return null;
  128.      }
  129.  
  130. Index: src/org/eclipse/dltk/ruby/typeinference/MethodCallTypeEvaluator.java
  131. ===================================================================
  132. RCS file: /cvsroot/technology/org.eclipse.dltk/ruby/plugins/org.eclipse.dltk.ruby.core/src/org/eclipse/dltk/ruby/typeinference/MethodCallTypeEvaluator.java,v
  133. retrieving revision 1.1
  134. diff -u -r1.1 MethodCallTypeEvaluator.java
  135. --- src/org/eclipse/dltk/ruby/typeinference/MethodCallTypeEvaluator.java    24 Feb 2007 18:56:59 -0000    1.1
  136. +++ src/org/eclipse/dltk/ruby/typeinference/MethodCallTypeEvaluator.java    24 Feb 2007 19:52:27 -0000
  137. @@ -1,22 +1,110 @@
  138.  package org.eclipse.dltk.ruby.typeinference;
  139.  
  140. +import java.util.List;
  141. +
  142. +import org.eclipse.dltk.ast.expressions.CallExpression;
  143. +import org.eclipse.dltk.ast.statements.Statement;
  144. +import org.eclipse.dltk.ddp.BasicContext;
  145.  import org.eclipse.dltk.ddp.ExpressionGoal;
  146.  import org.eclipse.dltk.ddp.GoalEvaluator;
  147.  import org.eclipse.dltk.ddp.IGoal;
  148.  import org.eclipse.dltk.evaluation.types.IEvaluatedType;
  149. +import org.eclipse.dltk.ruby.ast.SelfReference;
  150.  
  151.  public class MethodCallTypeEvaluator extends GoalEvaluator {
  152.  
  153. +    private final static int STATE_INIT = 0;
  154. +
  155. +    private final static int STATE_WAITING_RECEIVER = 1;
  156. +
  157. +    private final static int STATE_GOT_RECEIVER = 1;
  158. +
  159. +    private final static int STATE_WAITING_ARGUMENT_0 = 3;
  160. +
  161. +    private final static int STATE_ARGS_DONE = 10000;
  162. +
  163. +    private final static int STATE_WAITING_METHOD = 10001;
  164. +
  165. +    private final static int STATE_UNKNOWN = -1;
  166. +
  167. +    private final static int STATE_DONE = -2;
  168. +
  169. +    private int state = STATE_INIT;
  170. +
  171. +    private IEvaluatedType receiverType;
  172. +
  173. +    private IEvaluatedType[] arguments;
  174. +
  175. +    private IEvaluatedType result;
  176. +
  177.      public MethodCallTypeEvaluator(ExpressionGoal goal) {
  178.          super(goal);
  179.      }
  180.  
  181.      public IGoal produceNextSubgoal(IGoal previousGoal, IEvaluatedType previousResult) {
  182. +        if (state == STATE_INIT) {
  183. +            ExpressionGoal typedGoal = (ExpressionGoal) goal;
  184. +            CallExpression expression = (CallExpression) typedGoal.getExpression();
  185. +            Statement receiver = expression.getReceiver();
  186. +            if (receiver == null || receiver instanceof SelfReference) {
  187. +                // handling SelfReference here just for simplicity, could be
  188. +                // left to the TI engine as well
  189. +                receiverType = RubyTypeInferencingUtils.determineSelfClass(goal.getContext(),
  190. +                        expression.sourceStart());
  191. +                state = STATE_GOT_RECEIVER;
  192. +            } else {
  193. +                state = STATE_WAITING_RECEIVER;
  194. +                return new ExpressionGoal(goal.getContext(), receiver);
  195. +            }
  196. +        }
  197. +        if (state == STATE_WAITING_RECEIVER) {
  198. +            receiverType = previousResult;
  199. +            if (receiverType == null) {
  200. +                state = STATE_UNKNOWN;
  201. +                return null;
  202. +            }
  203. +            state = STATE_GOT_RECEIVER;
  204. +        }
  205. +        if (state == STATE_GOT_RECEIVER) {
  206. +            ExpressionGoal typedGoal = (ExpressionGoal) goal;
  207. +            CallExpression expression = (CallExpression) typedGoal.getExpression();
  208. +            List arguments = expression.getArgs().getExpressions();
  209. +            this.arguments = new IEvaluatedType[arguments.size()];
  210. +        }
  211. +        if (state >= STATE_WAITING_ARGUMENT_0) {
  212. +            arguments[state - STATE_WAITING_ARGUMENT_0] = previousResult;
  213. +        }
  214. +        if (state == STATE_GOT_RECEIVER || state >= STATE_WAITING_ARGUMENT_0) {
  215. +            int nextArg = (state == STATE_GOT_RECEIVER ? 0 : state - STATE_WAITING_ARGUMENT_0 + 1);
  216. +            ExpressionGoal typedGoal = (ExpressionGoal) goal;
  217. +            CallExpression expression = (CallExpression) typedGoal.getExpression();
  218. +            List arguments = expression.getArgs().getExpressions();
  219. +            if (nextArg < arguments.size()) {
  220. +                state = STATE_WAITING_ARGUMENT_0 + nextArg;
  221. +                return new ExpressionGoal(goal.getContext(), (Statement) arguments.get(nextArg));
  222. +            } else {
  223. +                state = STATE_ARGS_DONE;
  224. +            }
  225. +        }
  226. +        if (state == STATE_ARGS_DONE) {
  227. +            ExpressionGoal typedGoal = (ExpressionGoal) goal;
  228. +            CallExpression expression = (CallExpression) typedGoal.getExpression();
  229. +            state = STATE_WAITING_METHOD;
  230. +            return new MethodReturnTypeGoal(new InstanceContext((BasicContext) goal.getContext(),
  231. +                    receiverType), expression.getName(), arguments);
  232. +        }
  233. +        if (state == STATE_WAITING_METHOD) {
  234. +            result = previousResult;
  235. +            state = STATE_DONE;
  236. +        }
  237.          return null;
  238.      }
  239.  
  240.      public IEvaluatedType produceType() {
  241. -        return null;
  242. +        if (state == STATE_UNKNOWN)
  243. +            return null;
  244. +        else
  245. +            return result;
  246.      }
  247.  
  248.  }
  249.  

PermaLink to this entry https://pastebin.co.uk/10887
Posted by Andrey Tarantsov Sat 24th Feb 2007 19:56 - Syntax is Diff - 55 views
Download | New Post | Modify | Hide line numbers

 

Comments: 0