Skip to content

Commit

Permalink
use scope's getLocalType instead of TypeSystems
Browse files Browse the repository at this point in the history
  • Loading branch information
baroquebobcat committed Nov 5, 2016
1 parent 722bb8c commit d369e26
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 31 deletions.
16 changes: 9 additions & 7 deletions src/org/mirah/jvm/compiler/method_cleanup.mirah
Expand Up @@ -24,14 +24,14 @@ import org.mirah.jvm.types.JVMTypeUtils

# Runs class cleanup on any enclosed classes.
class MethodCleanup < NodeScanner
def initialize(context:Context, method:MethodDefinition)
def initialize(context: Context, method: MethodDefinition)
@context = context
@typer = context[Typer]
@scope = @typer.scoper.getIntroducedScope(method)
@method = method
end

def clean:void
def clean: void
scan(@method.body, nil)
end

Expand All @@ -46,13 +46,15 @@ class MethodCleanup < NodeScanner

def enterClosureDefinition(node, arg)
if @typer.getResolvedType(node).equals(@scope.binding_type) && node.body_size == 0
@scope.capturedLocals.each do |name|
type = JVMType(@typer.type_system.getLocalType(@scope, String(name), node.position).resolve)
if type.kind_of?(MirrorType)
type = JVMType(Object(MirrorType(type).erasure))
@scope.capturedLocals.each do |name: String|
type = @scope.getLocalType(name, node.position).resolve.as!(JVMType)

#type = JVMType(@typer.type_system.getLocalType(@scope, String(name), node.position).resolve)
if type.kind_of? MirrorType
type = type.as!(MirrorType).erasure.as!(Object).as!(JVMType)
end
typeref = TypeRefImpl.new(type.name, JVMTypeUtils.isArray(type), false, node.position)
decl = FieldDeclaration.new(SimpleString.new(String(name)), typeref, nil, [
decl = FieldDeclaration.new(SimpleString.new(name), typeref, nil, [
Annotation.new(SimpleString.new('org.mirah.jvm.types.Modifiers'), [
HashEntry.new(SimpleString.new('access'), SimpleString.new('PROTECTED')),
])
Expand Down
3 changes: 1 addition & 2 deletions src/org/mirah/jvm/compiler/method_compiler.mirah
Expand Up @@ -279,8 +279,7 @@ class MethodCompiler < BaseCompiler
if isCaptured
@builder.loadLocal(@binding)
end
future = typer.type_system.getLocalType(
getScope(local), name, local.position)
future = getScope(local).getLocalType(name, local.position)
raise "error type found by compiler #{future.resolve}" if future.resolve.kind_of? ErrorType

type = JVMType(
Expand Down
23 changes: 12 additions & 11 deletions src/org/mirah/jvm/mirrors/better_scope.mirah
Expand Up @@ -60,15 +60,16 @@ class Locals
implements Iterable
def initialize
@defined_locals = HashSet.new
@local_types = {}
@local_types = {}
end

def local_type(
name: String,
position: Position,
parent: BetterScope,
shadowed: boolean
)
type = LocalFuture(@local_types[name])
type = @local_types[name].as!(LocalFuture)
if type.nil?
type = LocalFuture.new(name, position)
locals = @defined_locals
Expand All @@ -81,7 +82,7 @@ class Locals
end

if parent && !shadowed
type.parent = BetterScope(parent).getLocalType(name, position)
type.parent = parent.as!(BetterScope).getLocalType(name, position)
end
@local_types[name] = type
end
Expand All @@ -101,8 +102,8 @@ class ImportsAndSearchPackages
attr_reader imports: Map, search_packages: List, staticImports: Set
def initialize
@search_packages = []
@imports = {}
@staticImports = HashSet.new
@imports = {}
@staticImports = HashSet.new
end

def add(fullname: String, shortname: String): void
Expand Down Expand Up @@ -186,8 +187,8 @@ class BetterScope

@parent.removeChild(self) if @parent

BetterScope(new_parent).addChild(self)
@parent = BetterScope(new_parent)
new_parent.as!(BetterScope).addChild(self)
@parent = new_parent.as!(BetterScope)

flush
end
Expand Down Expand Up @@ -298,12 +299,12 @@ class BetterScope
end

#mirrorscope overrides
def getLocalType(name: String, position: Position):LocalFuture; raise "no locals for #{getClass}.getLocalType" end
def getLocalType(name, position); raise "no locals for #{getClass}.getLocalType" end

def outer_scope: MirrorScope; raise "no outer_scope for #{getClass}" end

def staticImports: Set; raise "no staticImports for #{getClass}" end
def fetch_imports(something: Map): Map; raise "no fetch_imports imports for #{getClass}" end
def staticImports: Set; raise "no staticImports for #{getClass}" end
def fetch_imports(something: Map): Map; raise "no fetch_imports imports for #{getClass}" end
def fetch_static_imports(something: Set): Set; raise "no fetch_static_imports for #{getClass}" end

def fetch_packages(list: List): List; raise "no fetch_packages for #{getClass}" end
Expand Down Expand Up @@ -424,7 +425,7 @@ class BetterScope
macro def self.defers_locals
quote do
def getLocalType(name, position)
MirrorScope(parent).getLocalType(name, position)
parent.as!(MirrorScope).getLocalType(name, position)
end

def hasLocal(name, includeParent:boolean=true)
Expand Down
1 change: 0 additions & 1 deletion src/org/mirah/jvm/mirrors/mirror_scope.mirah
Expand Up @@ -33,7 +33,6 @@ import org.mirah.typer.TypeFuture
# mirror typesystem
interface MirrorScope < Scope

def getLocalType(name: String, position: Position):LocalFuture; end
# scope of the AST outside the current node. It may not be the parent scope.
def outer_scope: MirrorScope; end
# Currently available static imports
Expand Down
4 changes: 4 additions & 0 deletions src/org/mirah/typer/scopes.mirah
Expand Up @@ -16,6 +16,8 @@
package org.mirah.typer

import mirah.lang.ast.Node
import mirah.lang.ast.Position

import java.util.List
import java.util.Map

Expand Down Expand Up @@ -63,6 +65,8 @@ interface Scope do
def selfUsed(): void; end
def capturedSelf: boolean; end
def hasSelf: boolean; end

def getLocalType(name: String, position: Position):LocalFuture; end
end

interface Scoper do
Expand Down
24 changes: 16 additions & 8 deletions src/org/mirah/typer/simple/simple_scoper.mirah
Expand Up @@ -27,32 +27,38 @@ import java.io.FileInputStream
import java.io.PrintStream

interface ScopeFactory do
def newScope(scoper:Scoper, node:Node):Scope; end
def newScope(scoper: Scoper, node: Node): Scope; end
end

# A minimal Scoper.
class SimpleScoper; implements Scoper
attr_reader types: TypeSystem
def initialize
@scopes = {}
end
def initialize(factory:ScopeFactory)

def initialize(factory: ScopeFactory)#, types: TypeSystem)
@factory = factory
@scopes = {}
@types = types
end

def getScope(node)
orig = node
until node.parent.nil?
node = node.parent
scope = Scope(@scopes[node])
scope = getIntroducedScope node
return scope if scope
end
Scope(@scopes[node]) || addScope(node)
getIntroducedScope(node) || addScope(node)
end
def getIntroducedScope(node:Node)
Scope(@scopes[node])

def getIntroducedScope(node: Node)
@scopes[node].as! Scope
end

def addScope(node)
Scope(@scopes[node]) || begin
@scopes[node].as!(Scope) || begin
scope = if @factory
@factory.newScope(self, node)
else
Expand All @@ -62,9 +68,11 @@ class SimpleScoper; implements Scoper
scope
end
end
def setScope(node:Node, scope:Scope)

def setScope(node: Node, scope: Scope)
@scopes[node] = scope
end

def copyScopeFrom(from, to)
@scopes[to] = getScope(from)
end
Expand Down
4 changes: 2 additions & 2 deletions src/org/mirah/typer/typer.mirah
Expand Up @@ -744,7 +744,7 @@ class Typer < SimpleNodeVisitor
name = clause.name
if name
scope.shadow(name.identifier)
exceptionType = @types.getLocalType(scope, name.identifier, name.position)
exceptionType = scope.getLocalType(name.identifier, name.position)
clause.types.each do |_t|
t = TypeName(_t)
exceptionType.assign(inferTypeName(t), t.position)
Expand Down Expand Up @@ -1459,7 +1459,7 @@ class Typer < SimpleNodeVisitor
end

def getLocalType(arg: Node, identifier: String): AssignableTypeFuture
@types.getLocalType(scopeOf(arg), identifier, arg.position)
scopeOf(arg).getLocalType(identifier, arg.position)
end

def getArgumentType(arg: FormalArgument)
Expand Down

0 comments on commit d369e26

Please sign in to comment.