Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Truffle] Refactor the module/class declaration nodes.
  • Loading branch information
eregon committed Oct 22, 2014
1 parent cd315fa commit 001d617
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 46 deletions.
67 changes: 24 additions & 43 deletions core/src/main/java/org/jruby/truffle/translator/BodyTranslator.java
Expand Up @@ -560,10 +560,30 @@ public RubyNode visitCaseNode(org.jruby.ast.CaseNode node) {
}
}

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

final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(context, environment, environment.getParser(),
environment.getParser().allocateReturnID(), true, true, sharedMethodInfo, name, false);

final ModuleTranslator classTranslator = new ModuleTranslator(currentNode, context, this, newEnvironment, source);

final MethodDefinitionNode definitionMethod = classTranslator.compileClassNode(sourceSection, name, bodyNode);

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

@Override
public RubyNode visitClassNode(org.jruby.ast.ClassNode node) {
final SourceSection sourceSection = translate(node.getPosition());

final String name = node.getCPath().getName();

RubyNode lexicalParent = translateCPath(sourceSection, node.getCPath());

RubyNode superClass;
Expand All @@ -573,22 +593,9 @@ public RubyNode visitClassNode(org.jruby.ast.ClassNode node) {
superClass = new ObjectLiteralNode(context, sourceSection, context.getCoreLibrary().getObjectClass());
}

final DefineOrGetClassNode defineOrGetClass = new DefineOrGetClassNode(context, sourceSection, node.getCPath().getName(), lexicalParent, superClass);

LexicalScope newLexicalScope = context.pushLexicalScope();
try {
final SharedMethodInfo sharedMethodInfo = new SharedMethodInfo(sourceSection, newLexicalScope, node.getCPath().getName(), false, node.getBodyNode());

final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(context, environment, environment.getParser(), environment.getParser().allocateReturnID(), true, true,
sharedMethodInfo, sharedMethodInfo.getName(), false);
final ModuleTranslator classTranslator = new ModuleTranslator(currentNode, context, this, newEnvironment, source);
final DefineOrGetClassNode defineOrGetClass = new DefineOrGetClassNode(context, sourceSection, name, lexicalParent, superClass);

final MethodDefinitionNode definitionMethod = classTranslator.compileClassNode(node.getPosition(), sharedMethodInfo.getName(), node.getBodyNode());

return new OpenModuleNode(context, sourceSection, defineOrGetClass, definitionMethod);
} finally {
context.popLexicalScope();
}
return openModule(sourceSection, defineOrGetClass, name, node.getBodyNode());
}

private RubyNode translateCPath(SourceSection sourceSection, org.jruby.ast.Colon3Node node) {
Expand Down Expand Up @@ -1311,20 +1318,7 @@ public RubyNode visitModuleNode(org.jruby.ast.ModuleNode node) {

final DefineOrGetModuleNode defineModuleNode = new DefineOrGetModuleNode(context, sourceSection, name, lexicalParent);

LexicalScope newLexicalScope = context.pushLexicalScope();
try {
final SharedMethodInfo sharedMethodInfo = new SharedMethodInfo(sourceSection, newLexicalScope, name, false, node);

final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(
context, environment, environment.getParser(), environment.getParser().allocateReturnID(), true, true, sharedMethodInfo, name, false);
final ModuleTranslator classTranslator = new ModuleTranslator(currentNode, context, this, newEnvironment, source);

final MethodDefinitionNode definitionMethod = classTranslator.compileClassNode(node.getPosition(), name, node.getBodyNode());

return new OpenModuleNode(context, sourceSection, defineModuleNode, definitionMethod);
} finally {
context.popLexicalScope();
}
return openModule(sourceSection, defineModuleNode, name, node.getBodyNode());
}

@Override
Expand Down Expand Up @@ -1951,20 +1945,7 @@ public RubyNode visitSClassNode(org.jruby.ast.SClassNode node) {

final SingletonClassNode singletonClassNode = new SingletonClassNode(context, sourceSection, new BoxingNode(context, sourceSection, receiverNode));

LexicalScope newLexicalScope = context.pushLexicalScope();
try {
final SharedMethodInfo sharedMethodInfo = new SharedMethodInfo(sourceSection, newLexicalScope, "(singleton-def)", false, node);

final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(
context, environment, environment.getParser(), environment.getParser().allocateReturnID(), true, true, sharedMethodInfo, sharedMethodInfo.getName(), false);
final ModuleTranslator classTranslator = new ModuleTranslator(currentNode, context, this, newEnvironment, source);

final MethodDefinitionNode definitionMethod = classTranslator.compileClassNode(node.getPosition(), sharedMethodInfo.getName(), node.getBodyNode());

return new OpenModuleNode(context, sourceSection, singletonClassNode, definitionMethod);
} finally {
context.popLexicalScope();
}
return openModule(sourceSection, singletonClassNode, "(singleton-def)", node.getBodyNode());
}

@Override
Expand Down
Expand Up @@ -37,9 +37,7 @@ public ModuleTranslator(RubyNode currentNode, RubyContext context, BodyTranslato
useClassVariablesAsIfInClass = true;
}

public MethodDefinitionNode compileClassNode(ISourcePosition sourcePosition, String name, org.jruby.ast.Node bodyNode) {
final SourceSection sourceSection = translate(sourcePosition);

public MethodDefinitionNode compileClassNode(SourceSection sourceSection, String name, org.jruby.ast.Node bodyNode) {
environment.addMethodDeclarationSlots();

RubyNode body;
Expand Down

0 comments on commit 001d617

Please sign in to comment.