Class: Mirah::AST::StaticScope

Inherits:
Object
  • Object
show all
Defined in:
lib/mirah/ast/scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, parent = nil) ⇒ StaticScope

Returns a new instance of StaticScope.



61
62
63
64
65
66
67
68
69
70
# File 'lib/mirah/ast/scope.rb', line 61

def initialize(node, parent=nil)
  @scope_node = node
  @vars = {}
  @var_types = {}
  @parent = parent
  @children = {}
  @imports = {}
  @search_packages = []
  @shadowed = {}
end

Instance Attribute Details

#packageObject



187
188
189
# File 'lib/mirah/ast/scope.rb', line 187

def package
  @package || outer_scope.package
end

#parentObject

Returns the value of attribute parent.



58
59
60
# File 'lib/mirah/ast/scope.rb', line 58

def parent
  @parent
end

#self_nodeObject



152
153
154
155
156
157
# File 'lib/mirah/ast/scope.rb', line 152

def self_node
  if @self_node.nil? && parent
    @self_node = parent.self_node
  end
  @self_node
end

#self_typeObject



145
146
147
148
149
150
# File 'lib/mirah/ast/scope.rb', line 145

def self_type
  if @self_type.nil? && parent
    @self_type = parent.self_type
  end
  @self_type
end

Instance Method Details

#<<(name) ⇒ Object



72
73
74
# File 'lib/mirah/ast/scope.rb', line 72

def <<(name)
  @vars[name] = true
end

#add_child(scope) ⇒ Object



126
127
128
# File 'lib/mirah/ast/scope.rb', line 126

def add_child(scope)
  @children[scope] = true
end

#binding_type(defining_class = nil, mirah = nil) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/mirah/ast/scope.rb', line 159

def binding_type(defining_class=nil, mirah=nil)
  @binding_type ||= begin
    if parent
      parent.binding_type(defining_class, mirah)
    else
      name = "#{defining_class.name}$#{mirah.tmp}"
      factory = Mirah::AST.type_factory
      if factory
        factory.declare_type(@scope_node, name)
      else
        Mirah::AST::TypeReference.new(name, false, false)
      end
    end
  end
end

#binding_type=(type) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/mirah/ast/scope.rb', line 175

def binding_type=(type)
  if parent
    parent.binding_type = type
  else
    @binding_type = type
  end
end

#captured?(name) ⇒ Boolean

Returns:



112
113
114
115
116
117
118
119
120
# File 'lib/mirah/ast/scope.rb', line 112

def captured?(name)
  if !include?(name, false)
    return false
  elsif parent && parent.include?(name)
    return true
  else
    return children.any? {|child| child.include?(name, false)}
  end
end

#childrenObject



122
123
124
# File 'lib/mirah/ast/scope.rb', line 122

def children
  @children.keys
end

#fetch_imports(map) ⇒ Object



191
192
193
194
195
196
# File 'lib/mirah/ast/scope.rb', line 191

def fetch_imports(map)
  parent_scope = outer_scope
  parent_scope.fetch_imports(map) if parent_scope

  map.update(@imports)
end

#fetch_packages(list) ⇒ Object



198
199
200
201
202
203
# File 'lib/mirah/ast/scope.rb', line 198

def fetch_packages(list)
  parent_scope = outer_scope
  parent_scope.fetch_packages(list) if parent_scope

  list.concat(@search_packages)
end

#has_binding?Boolean

Returns:



183
184
185
# File 'lib/mirah/ast/scope.rb', line 183

def has_binding?
  @binding_type != nil || (parent && parent.has_binding?)
end

#import(full_name, short_name) ⇒ Object



213
214
215
216
217
218
219
220
# File 'lib/mirah/ast/scope.rb', line 213

def import(full_name, short_name)
  return if full_name == short_name
  if short_name == '*'
    @search_packages << full_name.sub(/\.\*$/, '')
  else
    @imports[short_name] = full_name
  end
end

#importsObject



205
206
207
# File 'lib/mirah/ast/scope.rb', line 205

def imports
  @cached_imports ||= fetch_imports({})
end

#include?(name, include_parent = true) ⇒ Boolean

Returns:



107
108
109
110
# File 'lib/mirah/ast/scope.rb', line 107

def include?(name, include_parent=true)
  @vars.include?(name) ||
      (include_parent && parent && parent.include?(name))
end

#learn_local_type(name, type) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mirah/ast/scope.rb', line 92

def learn_local_type(name, type)
  return unless type
  existing_type = local_type(name)
  if existing_type
    unless existing_type.compatible?(type)
      raise Mirah::Typer::InferenceError.new(
          "Can't assign #{type.full_name} to " \
          "variable of type #{existing_type.full_name}")
    end
    existing_type
  elsif type
    @var_types[name] = type
  end
end

#local_type(name) ⇒ Object



88
89
90
# File 'lib/mirah/ast/scope.rb', line 88

def local_type(name)
  @var_types[name]
end

#localsObject



84
85
86
# File 'lib/mirah/ast/scope.rb', line 84

def locals
  @vars.keys
end

#outer_scopeObject



140
141
142
143
# File 'lib/mirah/ast/scope.rb', line 140

def outer_scope
  node = @scope_node.scope
  node && node.static_scope
end

#remove_child(scope) ⇒ Object



130
131
132
# File 'lib/mirah/ast/scope.rb', line 130

def remove_child(scope)
  @children.delete(scope)
end

#search_packagesObject



209
210
211
# File 'lib/mirah/ast/scope.rb', line 209

def search_packages
  @cached_packages ||= fetch_packages([])
end

#shadow(name) ⇒ Object



76
77
78
# File 'lib/mirah/ast/scope.rb', line 76

def shadow(name)
  @shadowed[name] = @vars[name] = true
end

#shadowed?(name) ⇒ Boolean

Returns:



80
81
82
# File 'lib/mirah/ast/scope.rb', line 80

def shadowed?(name)
  @shadowed[name]
end