-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathclass.rb
More file actions
351 lines (305 loc) · 9.34 KB
/
Copy pathclass.rb
File metadata and controls
351 lines (305 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# Copyright (c) 2010 The Mirah project authors. All Rights Reserved.
# All contributing project authors may be found in the NOTICE file.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
module Mirah::AST
class ClassDefinition < Node
include Annotated
include Named
include Scope
include Java::DubyLangCompiler.ClassDefinition
attr_accessor :interfaces
attr_accessor :current_access_level
attr_accessor :abstract
child :superclass_node
child :body
attr_accessor :superclass
def initialize(parent, position, name, annotations=[], &block)
@abstract = false
@annotations = annotations
@interfaces = []
@interface_nodes = []
self.name = name
self.parent = parent
if Mirah::AST.type_factory.respond_to? :define_type
Mirah::AST.type_factory.define_type(self)
end
# We need somewhere to collect nodes that get appended during
# the transform phase.
@extra_body = Body.new(self, position)
super(parent, position, &block)
if body
@extra_body.insert(0, body)
end
self.body = @extra_body
end
def append_node(node)
@extra_body << node
node
end
def define_method(position, name, type, *args, &block)
append_node(_define_method(MethodDefinition, position, name, type, args, &block))
end
def define_static_method(position, name, type, *args, &block)
append_node(
_define_method(StaticMethodDefinition, position, name, type, args, &block))
end
def define_constructor(position, *args, &block)
append_node(_define_method(
ConstructorDefinition, position, 'initialize', nil, args, &block))
end
def _define_method(klass, position, name, type, args)
klass.new(nil, position, name) do |method|
signature = {:return => type}
if Arguments === args[0]
args_node = args[0]
args_node.parent = method
else
args_node = Arguments.new(method, position) do |args_node|
arg_list = args.map do |arg_name, arg_type, arg_position|
signature[arg_name.intern] = arg_type
arg_position ||= position
RequiredArgument.new(args_node, arg_position, arg_name)
end
[arg_list, nil, nil, nil]
end
end
[
signature,
args_node,
if block_given?
yield(method)
end
]
end
end
def declare_field(position, name, type)
field = FieldDeclaration.new(nil, position || self.position, name)
field.type = type.dup
append_node(field)
end
def infer(typer, expression)
resolve_if(typer) do
@superclass = superclass_node.type_reference(typer) if superclass_node
@annotations.each {|a| a.infer(typer, true)} if @annotations
@interfaces.concat(@interface_nodes.map{|n| n.type_reference(typer)})
typer.define_type(self, name, superclass, @interfaces) do
static_scope.self_type = typer.self_type
typer.infer(body, false) if body
end
end
end
def implements(*types)
raise ArgumentError if types.any? {|x| x.nil?}
types.each do |type|
if Mirah::AST::TypeReference === type
@interfaces << type
else
@interface_nodes << type
end
end
end
def top_level?
true
end
end
defmacro('implements') do |transformer, fcall, parent|
klass = parent
klass = klass.parent unless ClassDefinition === klass
interfaces = fcall.parameters.map do |interface|
interface.parent = klass
interface
end
klass.implements(*interfaces)
Noop.new(parent, fcall.position)
end
class InterfaceDeclaration < ClassDefinition
attr_accessor :superclass, :interfaces
child :interface_nodes
child :body
def initialize(parent, position, name, annotations)
super(parent, position, name, annotations) {|p| }
@abstract = true
self.name = name
@children = [[], nil]
@children = yield(self)
end
def infer(typer, expression)
resolve_if(typer) do
@interfaces = interface_nodes.map {|i| i.type_reference(typer)}
super
end
end
def superclass_node
nil
end
def top_level?
true
end
end
class ClosureDefinition < ClassDefinition
attr_accessor :enclosing_type
def initialize(parent, position, name, enclosing_type)
super(parent, position, name, []) do
[nil, nil]
end
@enclosing_type = enclosing_type
end
end
defmacro('interface') do |transformer, fcall, parent|
raise Mirah::SyntaxError.new("Interface name required", fcall) unless fcall.parameters.size > 0
interfaces = fcall.parameters
interface_name = interfaces.shift
if (Call === interface_name &&
interface_name.parameters.size == 1)
interfaces.unshift(interface_name.parameters[0])
interface_name = interface_name.target
end
raise 'Interface body required' unless fcall.block
InterfaceDeclaration.new(parent, fcall.position,
interface_name.name,
transformer.annotations) do |interface|
interfaces.each {|x| x.parent = interface}
[interfaces,
if fcall.block.body
fcall.block.body.parent = interface
fcall.block.body
end
]
end
end
class FieldDeclaration < Node
include Annotated
include Named
include ClassScoped
include Typed
child :type_node
attr_accessor :type
attr_accessor :static
def initialize(parent, position, name, annotations=[], static = false, &block)
@annotations = annotations
super(parent, position, &block)
self.name = name
@static = static
end
def infer(typer, expression)
resolve_if(typer) do
@annotations.each {|a| a.infer(typer, true)} if @annotations
@type = type_node.type_reference(typer)
end
end
def resolved!(typer)
if static
typer.learn_static_field_type(class_scope, name, @inferred_type)
else
typer.learn_field_type(class_scope, name, @inferred_type)
end
super
end
end
class FieldAssignment < Node
include Annotated
include Named
include Valued
include ClassScoped
child :value
attr_accessor :static
def initialize(parent, position, name, annotations=[], static = false, &block)
@annotations = annotations
super(parent, position, &block)
self.name = name
@static = static
end
def infer(typer, expression)
resolve_if(typer) do
@annotations.each {|a| a.infer(typer, true)} if @annotations
if static
typer.learn_static_field_type(class_scope, name, typer.infer(value, true))
else
typer.learn_field_type(class_scope, name, typer.infer(value, true))
end
end
end
end
class Field < Node
include Annotated
include Named
include ClassScoped
attr_accessor :static
def initialize(parent, position, name, annotations=[], static = false, &block)
@annotations = annotations
super(parent, position, &block)
self.name = name
@static = static
end
def infer(typer, expression)
resolve_if(typer) do
@annotations.each {|a| a.infer(typer, true)} if @annotations
if static
typer.static_field_type(class_scope, name)
else
typer.field_type(class_scope, name)
end
end
end
end
class AccessLevel < Node
include ClassScoped
include Named
def initialize(parent, line_number, name)
super(parent, line_number)
self.name = name
class_scope.current_access_level = name.to_sym
end
def infer(typer, expression)
typer.no_type
end
end
class Include < Node
include Scoped
def infer(typer, expression)
children.each do |type|
typeref = type.type_reference(typer)
the_scope = scope.static_scope
the_scope.self_type = the_scope.self_type.include(typeref)
end
end
end
defmacro("include") do |transformer, fcall, parent|
raise "Included Class name required" unless fcall.parameters.size > 0
Include.new(parent, fcall.position) do |include_node|
fcall.parameters.map do |constant|
constant.parent = include_node
constant
end
end
end
class Constant < Node
include Named
include Scoped
attr_accessor :array
def initialize(parent, position, name)
self.name = name
super(parent, position, [])
end
def infer(typer, expression)
@inferred_type ||= begin
# TODO lookup constant, inline if we're supposed to.
typer.type_reference(scope, name, @array, true)
end
end
def type_reference(typer)
typer.type_reference(scope, @name, @array)
end
end
end