Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Truffle] Move lexical scope state to the ParseEnvironment, a per-par…
…se TranslatorEnvironment.
  • Loading branch information
eregon committed Nov 5, 2014
1 parent 624823a commit aaa9628
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
25 changes: 5 additions & 20 deletions core/src/main/java/org/jruby/truffle/runtime/RubyContext.java
Expand Up @@ -62,9 +62,6 @@ public class RubyContext extends ExecutionContext {
private final Warnings warnings;
private final SafepointManager safepointManager;
private final Random random = new Random();

// TODO: Wrong place for this, only during parsing but be practical for now
private LexicalScope lexicalScope;
private final LexicalScope rootLexicalScope;

private SourceCallback sourceCallback = null;
Expand Down Expand Up @@ -107,23 +104,7 @@ public RubyContext(Ruby runtime) {
threadManager = new ThreadManager(this);
fiberManager = new FiberManager(this);

lexicalScope = rootLexicalScope = new LexicalScope(null, coreLibrary.getObjectClass());
}

public LexicalScope getRootLexicalScope() {
return rootLexicalScope;
}

public LexicalScope getLexicalScope() {
return lexicalScope;
}

public LexicalScope pushLexicalScope() {
return lexicalScope = new LexicalScope(lexicalScope);
}

public void popLexicalScope() {
lexicalScope = lexicalScope.getParent();
rootLexicalScope = new LexicalScope(null, coreLibrary.getObjectClass());
}

public void load(Source source, RubyNode currentNode) {
Expand Down Expand Up @@ -396,4 +377,8 @@ public SafepointManager getSafepointManager() {
public Random getRandom() {
return random;
}

public LexicalScope getRootLexicalScope() {
return rootLexicalScope;
}
}
Expand Up @@ -561,7 +561,7 @@ public RubyNode visitCaseNode(org.jruby.ast.CaseNode node) {
}

private RubyNode openModule(SourceSection sourceSection, RubyNode defineOrGetNode, String name, Node bodyNode) {
LexicalScope newLexicalScope = context.pushLexicalScope();
LexicalScope newLexicalScope = environment.pushLexicalScope();
try {
final SharedMethodInfo sharedMethodInfo = new SharedMethodInfo(sourceSection, newLexicalScope, name, false, bodyNode);

Expand All @@ -574,7 +574,7 @@ private RubyNode openModule(SourceSection sourceSection, RubyNode defineOrGetNod

return new OpenModuleNode(context, sourceSection, defineOrGetNode, definitionMethod);
} finally {
context.popLexicalScope();
environment.popLexicalScope();
}
}

Expand All @@ -600,7 +600,7 @@ public RubyNode visitClassNode(org.jruby.ast.ClassNode node) {

private RubyNode translateCPath(SourceSection sourceSection, org.jruby.ast.Colon3Node node) {
if (node instanceof Colon2ImplicitNode) { // use current lexical scope
return new LexicalScopeNode(context, sourceSection, context.getLexicalScope());
return new LexicalScopeNode(context, sourceSection, environment.getLexicalScope());
} else if (node instanceof Colon2ConstNode) { // A::B
return node.childNodes().get(0).accept(this);
} else { // Colon3Node: on top-level (Object)
Expand Down Expand Up @@ -662,7 +662,7 @@ public RubyNode visitConstDeclNode(org.jruby.ast.ConstDeclNode node) {
RubyNode moduleNode;
Node constNode = node.getConstNode();
if (constNode == null || constNode instanceof Colon2ImplicitNode) {
moduleNode = new LexicalScopeNode(context, sourceSection, context.getLexicalScope());
moduleNode = new LexicalScopeNode(context, sourceSection, environment.getLexicalScope());
} else if (constNode instanceof Colon2ConstNode) {
constNode = ((Colon2Node) constNode).getLeftNode(); // Misleading doc, we only want the defined part.
moduleNode = constNode.accept(this);
Expand All @@ -679,7 +679,7 @@ public RubyNode visitConstDeclNode(org.jruby.ast.ConstDeclNode node) {
public RubyNode visitConstNode(org.jruby.ast.ConstNode node) {
final SourceSection sourceSection = translate(node.getPosition());

RubyNode moduleNode = new LexicalScopeNode(context, sourceSection, context.getLexicalScope());
RubyNode moduleNode = new LexicalScopeNode(context, sourceSection, environment.getLexicalScope());

return new ReadConstantNode(context, sourceSection, node.getName(), moduleNode);
}
Expand Down
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.translator;

import org.jruby.truffle.runtime.LexicalScope;
import org.jruby.truffle.runtime.RubyContext;

/**
* Translator environment, unique per parse/translation.
*/
public class ParseEnvironment {

private LexicalScope lexicalScope;

public ParseEnvironment(RubyContext context) {
lexicalScope = context.getRootLexicalScope();
}

public LexicalScope getLexicalScope() {
return lexicalScope;
}

public LexicalScope pushLexicalScope() {
return lexicalScope = new LexicalScope(lexicalScope);
}

public void popLexicalScope() {
lexicalScope = lexicalScope.getParent();
}

}
Expand Up @@ -12,8 +12,6 @@
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

import com.oracle.truffle.api.*;
import com.oracle.truffle.api.impl.DefaultFrameTypeConversion;
import com.oracle.truffle.api.source.*;
import com.oracle.truffle.api.frame.*;
import org.jruby.truffle.nodes.*;
Expand All @@ -26,6 +24,8 @@ public class TranslatorEnvironment {

private final RubyContext context;

private final ParseEnvironment parseEnvironment;

private final FrameDescriptor frameDescriptor;

private final List<FrameSlot> flipFlopStates = new ArrayList<>();
Expand Down Expand Up @@ -60,13 +60,26 @@ public TranslatorEnvironment(RubyContext context, TranslatorEnvironment parent,
this.sharedMethodInfo = sharedMethodInfo;
this.namedMethodName = namedMethodName;
this.isBlock = isBlock;
this.parseEnvironment = (parent != null ? parent.parseEnvironment : new ParseEnvironment(context));
}

public TranslatorEnvironment(RubyContext context, TranslatorEnvironment parent, TranslatorDriver parser, long returnID, boolean ownScopeForAssignments, boolean neverAssignInParentScope,
SharedMethodInfo methodIdentifier, String namedMethodName, boolean isBlock) {
this(context, parent, new FrameDescriptor(new RubyFrameTypeConversion(context.getCoreLibrary().getNilObject())), parser, returnID, ownScopeForAssignments, neverAssignInParentScope, methodIdentifier, namedMethodName, isBlock);
}

public LexicalScope getLexicalScope() {
return parseEnvironment.getLexicalScope();
}

public LexicalScope pushLexicalScope() {
return parseEnvironment.pushLexicalScope();
}

public void popLexicalScope() {
parseEnvironment.popLexicalScope();
}

public TranslatorEnvironment getParent() {
return parent;
}
Expand Down

0 comments on commit aaa9628

Please sign in to comment.