mirah 0.1.2-java → 0.1.3-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/History.txt +225 -0
  3. data/Rakefile +108 -315
  4. data/TODO.md +100 -0
  5. data/bin/bundler +16 -0
  6. data/bin/rake +16 -0
  7. data/dist/mirahc.jar +0 -0
  8. data/examples/appengine/Readme +0 -1
  9. data/examples/literals.mirah +17 -0
  10. data/examples/macros/string_each_char.mirah +1 -1
  11. data/lib/mirah.rb +11 -21
  12. data/lib/mirah/transform/transformer.rb +1 -2
  13. data/lib/mirah/util/class_loader.rb +1 -1
  14. data/lib/mirah/util/logging.rb +0 -63
  15. data/lib/mirah/util/process_errors.rb +1 -0
  16. data/lib/mirah/version.rb +1 -1
  17. data/{examples/simple_class.mirah~ → test/artifacts/jar_test.rb} +7 -11
  18. data/{lib/mirah/commands.rb → test/artifacts/jruby_test.rb} +8 -5
  19. data/test/core/typer_test.rb +29 -11
  20. data/test/core/util/argument_processor_test.rb +24 -23
  21. data/test/core/util/class_loader_test.rb +7 -4
  22. data/test/core/util/{compilation_state_test.rb → jvm_version_test.rb} +20 -16
  23. data/test/fixtures/org/foo/ImplicitClassRetAnno.java +4 -0
  24. data/test/fixtures/org/foo/IntAnno.java +9 -0
  25. data/test/jvm/annotations_test.rb +11 -11
  26. data/test/jvm/blocks_test.rb +16 -12
  27. data/test/jvm/constructors_test.rb +8 -8
  28. data/test/jvm/enumerable_test.rb +48 -24
  29. data/test/jvm/generics_test.rb +3 -7
  30. data/test/jvm/import_test.rb +14 -0
  31. data/test/jvm/interface_test.rb +9 -24
  32. data/test/jvm/jvm_commands_test.rb +22 -4
  33. data/test/jvm/jvm_compiler_test.rb +124 -79
  34. data/test/jvm/list_extensions_test.rb +1 -1
  35. data/test/jvm/macros_test.rb +67 -14
  36. data/test/jvm/main_method_test.rb +1 -1
  37. data/test/jvm/new_backend_test_helper.rb +100 -3
  38. data/{lib/mirah/jvm/types/bitescript_ext.rb → test/jvm/static_fields_test.rb} +22 -21
  39. data/test/mirrors/base_type_test.rb +4 -3
  40. data/test/mirrors/bytecode_mirror_test.rb +35 -15
  41. data/test/mirrors/generics_test.rb +14 -5
  42. data/test/mirrors/member_test.rb +2 -1
  43. data/test/mirrors/method_lookup_test.rb +18 -6
  44. data/test/mirrors/mirrors_test.rb +87 -20
  45. data/test/mirrors/simple_async_mirror_loader_test.rb +7 -3
  46. data/test/mirrors/simple_mirror_loader_test.rb +5 -5
  47. data/test/test_helper.rb +25 -1
  48. metadata +18 -78
  49. data/bin/mirahp +0 -27
  50. data/bin/mirahp.cmd +0 -16
  51. data/examples/Fib.class +0 -0
  52. data/javalib/mirah-bootstrap.jar +0 -0
  53. data/javalib/mirah-builtins.jar +0 -0
  54. data/javalib/mirah-compiler.jar +0 -0
  55. data/javalib/mirah-mirrors.jar +0 -0
  56. data/javalib/mirah-newast-transitional.jar +0 -0
  57. data/javalib/mirah-parser.jar +0 -0
  58. data/javalib/mirah-util.jar +0 -0
  59. data/lib/mirah/ast.rb +0 -43
  60. data/lib/mirah/ast/scope.rb +0 -262
  61. data/lib/mirah/commands/base.rb +0 -59
  62. data/lib/mirah/commands/compile.rb +0 -39
  63. data/lib/mirah/commands/parse.rb +0 -36
  64. data/lib/mirah/commands/run.rb +0 -78
  65. data/lib/mirah/generator.rb +0 -150
  66. data/lib/mirah/jvm/compiler.rb +0 -50
  67. data/lib/mirah/jvm/compiler/base.rb +0 -421
  68. data/lib/mirah/jvm/compiler/jvm_bytecode.rb +0 -1194
  69. data/lib/mirah/jvm/method_lookup.rb +0 -307
  70. data/lib/mirah/jvm/types.rb +0 -45
  71. data/lib/mirah/jvm/types/array_type.rb +0 -60
  72. data/lib/mirah/jvm/types/ast_ext.rb +0 -31
  73. data/lib/mirah/jvm/types/basic_types.rb +0 -41
  74. data/lib/mirah/jvm/types/block_type.rb +0 -15
  75. data/lib/mirah/jvm/types/boolean.rb +0 -70
  76. data/lib/mirah/jvm/types/enumerable.rb +0 -80
  77. data/lib/mirah/jvm/types/extensions.rb +0 -110
  78. data/lib/mirah/jvm/types/factory.rb +0 -830
  79. data/lib/mirah/jvm/types/floats.rb +0 -99
  80. data/lib/mirah/jvm/types/generic_type.rb +0 -72
  81. data/lib/mirah/jvm/types/implicit_nil_type.rb +0 -29
  82. data/lib/mirah/jvm/types/integers.rb +0 -131
  83. data/lib/mirah/jvm/types/interface_definition.rb +0 -20
  84. data/lib/mirah/jvm/types/intrinsics.rb +0 -385
  85. data/lib/mirah/jvm/types/literals.rb +0 -89
  86. data/lib/mirah/jvm/types/meta_type.rb +0 -54
  87. data/lib/mirah/jvm/types/methods.rb +0 -946
  88. data/lib/mirah/jvm/types/null_type.rb +0 -39
  89. data/lib/mirah/jvm/types/number.rb +0 -184
  90. data/lib/mirah/jvm/types/primitive_type.rb +0 -76
  91. data/lib/mirah/jvm/types/source_mirror.rb +0 -274
  92. data/lib/mirah/jvm/types/type.rb +0 -311
  93. data/lib/mirah/jvm/types/type_definition.rb +0 -72
  94. data/lib/mirah/jvm/types/void_type.rb +0 -19
  95. data/lib/mirah/util/compilation_state.rb +0 -60
  96. data/test/core/commands_test.rb +0 -89
  97. data/test/core/generator_test.rb +0 -26
  98. data/test/fixtures/org/foo/LowerCaseInnerClass$inner.class +0 -0
  99. data/test/fixtures/org/foo/LowerCaseInnerClass.class +0 -0
  100. data/test/jvm/bytecode_test_helper.rb +0 -193
  101. data/test/jvm/factory_test.rb +0 -28
  102. data/test/jvm/java_typer_test.rb +0 -283
@@ -1,89 +0,0 @@
1
- # Copyright (c) 2010 The Mirah project authors. All Rights Reserved.
2
- # All contributing project authors may be found in the NOTICE file.
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
-
16
- require 'mirah/util/delegate'
17
-
18
- module Mirah::JVM::Types
19
-
20
- # Represents a literal number that can be represented
21
- # in multiple types
22
- class NarrowingType < Mirah::Util::DelegateClass(PrimitiveType)
23
- def initialize(default_type, narrowed_type)
24
- super(default_type)
25
- @narrowed = default_type != narrowed_type && narrowed_type
26
- end
27
-
28
- def hash
29
- __getobj__.hash
30
- end
31
-
32
- # Changes this type to the smallest type that will hold
33
- # its literal value.
34
- def narrow!
35
- if @narrowed
36
- __setobj__(@narrowed)
37
- true
38
- end
39
- end
40
- end
41
-
42
- class FixnumLiteral < NarrowingType
43
- def self.range(type)
44
- type::MIN_VALUE .. type::MAX_VALUE
45
- end
46
-
47
- BYTE_RANGE = range(java.lang.Byte)
48
- SHORT_RANGE = range(java.lang.Short)
49
- INT_RANGE = range(java.lang.Integer)
50
- LONG_RANGE = range(java.lang.Long)
51
-
52
- def initialize(literal)
53
- default_type = case literal
54
- when INT_RANGE
55
- Int
56
- else
57
- Long
58
- end
59
-
60
- # TODO chars?
61
- # There's not really any way to tell if we should narrow to a char
62
- # or a byte/short. I suppose we could try both, but that seems ugly.
63
- # Maybe it's the right thing to do though?
64
- narrowed_type = case literal
65
- when BYTE_RANGE
66
- Byte
67
- when SHORT_RANGE
68
- Short
69
- when INT_RANGE
70
- Int
71
- else
72
- Long
73
- end
74
-
75
- super(default_type, narrowed_type)
76
- end
77
- end
78
-
79
- class FloatLiteral < NarrowingType
80
- FLOAT_RANGE = java.lang.Float::MIN_VALUE .. java.lang.Float::MAX_VALUE
81
- NaN = java.lang.Float::NaN
82
- POSITIVE_INFINITY = java.lang.Float::POSITIVE_INFINITY
83
- NEGATIVE_INFINITY = java.lang.Float::NEGATIVE_INFINITY
84
-
85
- def initialize(literal)
86
- super(Double, Double)
87
- end
88
- end
89
- end
@@ -1,54 +0,0 @@
1
- module Mirah
2
- module JVM
3
- module Types
4
- class MetaType < Type
5
- attr_reader :unmeta
6
-
7
- def initialize(unmeta)
8
- super(unmeta.type_system, unmeta.name)
9
- @unmeta = unmeta
10
- end
11
-
12
- def basic_type
13
- @unmeta.basic_type
14
- end
15
-
16
- def name
17
- return @unmeta.name if @unmeta
18
- super
19
- end
20
-
21
- def meta?
22
- true
23
- end
24
-
25
- def meta
26
- self
27
- end
28
-
29
- def superclass
30
- if @unmeta.superclass
31
- @unmeta.superclass.meta
32
- else
33
- nil
34
- end
35
- end
36
-
37
- def interfaces(include_parent=true)
38
- []
39
- end
40
-
41
- def jvm_type
42
- unmeta.jvm_type
43
- end
44
-
45
- def inner_class?
46
- basic_type.inner_class?
47
- end
48
- end
49
-
50
- class TypeDefMeta < MetaType
51
- end
52
- end
53
- end
54
- end
@@ -1,946 +0,0 @@
1
- # Copyright (c) 2010-2013 The Mirah project authors. All Rights Reserved.
2
- # All contributing project authors may be found in the NOTICE file.
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
-
16
- require 'mirah/jvm/types'
17
-
18
- class Java::JavaMethod
19
- def static?
20
- java.lang.reflect.Modifier.static?(modifiers)
21
- end
22
-
23
- def abstract?
24
- java.lang.reflect.Modifier.abstract?(modifiers)
25
- end
26
- end
27
-
28
- class BiteScript::ASM::FieldMirror
29
- def returnType
30
- self.type
31
- end
32
- end
33
-
34
- module Mirah::JVM::Types
35
- AST ||= Mirah::AST
36
-
37
- module ArgumentConversion
38
- def convert_args(compiler, values, types=nil)
39
- # TODO boxing/unboxing
40
- types ||= argument_types
41
- needs_to_build_varargs_array = false
42
-
43
- if respond_to?(:varargs?) && varargs?
44
- non_varargs_types = types[0..-2]
45
- non_varargs_values = values.first non_varargs_types.size
46
-
47
- varargs_values = values.to_a.last(values.size - non_varargs_values.size)
48
- varargs_type = types.last
49
-
50
- unless varargs_values.length == 1 &&
51
- varargs_type.compatible?(compiler.inferred_type(varargs_values.first))
52
- needs_to_build_varargs_array = true
53
- values = non_varargs_values
54
- end
55
- end
56
-
57
- values_and_types = values.zip(types)
58
-
59
- values_and_types.each do |value, type|
60
- compiler.visit(value, true)
61
- in_type = compiler.inferred_type(value)
62
- if in_type.primitive? && type != in_type
63
- in_type.compile_widen(compiler.method, type)
64
- end
65
- end
66
-
67
- if needs_to_build_varargs_array
68
- compiler.visitVarargsArray(varargs_type, varargs_values)
69
- end
70
- end
71
- end
72
-
73
- Type.send :include, ArgumentConversion
74
-
75
- class Intrinsic
76
- include ArgumentConversion
77
- attr_reader :name, :argument_types, :return_type
78
-
79
- def initialize(klass, name, args, type, kind=nil, &block)
80
- raise ArgumentError, "Block required" unless block_given?
81
- @class = klass
82
- @name = name
83
- @argument_types = args
84
- @return_type = type
85
- @block = block
86
- @kind = kind
87
- end
88
-
89
- def kind
90
- Java::OrgMirahJvmTypes::MemberKind.const_get(@kind)
91
- end
92
-
93
- def accept(visitor, expression)
94
- name = "visit_#{@kind.downcase}".sub(/_op$/,"")
95
- visitor.send(name, self, expression)
96
- end
97
-
98
- def call(builder, ast, expression, *args)
99
- @block.call(builder, ast, expression, *args)
100
- end
101
-
102
- def declaring_class
103
- @class
104
- end
105
-
106
- def constructor?
107
- false
108
- end
109
-
110
- def field?
111
- false
112
- end
113
-
114
- def abstract?
115
- false
116
- end
117
-
118
- def exceptions
119
- []
120
- end
121
-
122
- def varargs?
123
- false
124
- end
125
-
126
- def isVararg
127
- varargs?
128
- end
129
- end
130
-
131
- class Macro
132
- java_import 'org.mirah.typer.InlineCode'
133
- java_import 'org.mirah.typer.NodeBuilder'
134
- attr_reader :name, :argument_types, :return_type
135
-
136
- def initialize(klass, name, args, &block)
137
- raise ArgumentError, "Block required" unless block_given?
138
- @class = klass
139
- @name = name
140
- @argument_types = args
141
- raise ArgumentError unless args.all?
142
- @return_type = InlineCode.new(block.to_java(NodeBuilder))
143
- end
144
-
145
- def declaring_class
146
- @class
147
- end
148
- end
149
-
150
- class JavaCallable
151
- include ArgumentConversion
152
-
153
- attr_accessor :member
154
-
155
- def initialize(types, member)
156
- raise ArgumentError unless types.kind_of?(Mirah::Typer::TypeSystem)
157
- @types = types
158
- @member = member
159
- end
160
-
161
- def name
162
- @name ||= @member.name
163
- end
164
-
165
- def field?
166
- false
167
- end
168
-
169
- def parameter_types
170
- @member.parameter_types
171
- end
172
-
173
- def synthetic?
174
- @member.synthetic?
175
- end
176
- end
177
-
178
- class JavaConstructor < JavaCallable
179
- def argument_types
180
- @argument_types ||= @member.argument_types.map do |arg|
181
- if arg.kind_of?(Type) || arg.nil?
182
- arg
183
- else
184
- @types.type(nil, arg)
185
- end
186
- end
187
- end
188
-
189
- def return_type
190
- declaring_class
191
- end
192
-
193
- def exceptions
194
- @member.exception_types.map do |exception|
195
- if exception.kind_of?(Mirah::Typer::ResolvedType)
196
- exception
197
- else
198
- @types.type(nil, exception.class_name)
199
- end
200
- end
201
- end
202
-
203
- def declaring_class
204
- @types.type(nil, @member.declaring_class)
205
- end
206
-
207
- def type_parameters
208
- @declaring_class and @declaring_class.jvm_type.type_parameters
209
- end
210
-
211
- def call(compiler, ast, expression, parameters=nil, delegate=false)
212
- target = compiler.inferred_type(ast.target)
213
- unless delegate
214
- compiler.method.new target
215
- compiler.method.dup if expression
216
- end
217
- parameters ||= ast.parameters
218
- convert_args(compiler, parameters)
219
- compiler.method.invokespecial(
220
- target,
221
- "<init>",
222
- [nil, *@member.argument_types])
223
- end
224
-
225
- def constructor?
226
- true
227
- end
228
-
229
- def varargs?
230
- @member.varargs?
231
- end
232
-
233
- def isVararg
234
- varargs?
235
- end
236
-
237
- def accept(visitor, expression)
238
- visitor.visitConstructor(self, expression)
239
- end
240
-
241
- def kind
242
- Java::OrgMirahJvmTypes::MemberKind::CONSTRUCTOR
243
- end
244
- end
245
-
246
- class JavaMethod < JavaConstructor
247
- def return_type
248
- @return_type ||= begin
249
- if void?
250
- @types.type(nil, 'void')
251
- else
252
- @types.type(nil, @member.return_type)
253
- end
254
- end
255
- end
256
-
257
- def static?
258
- @member.static?
259
- end
260
-
261
- def abstract?
262
- @member.abstract?
263
- end
264
-
265
- def type_parameters
266
- @member.type_parameters
267
- end
268
-
269
- def void?
270
- return_type = @member.return_type
271
- return true if return_type.nil?
272
- if return_type.respond_to?(:descriptor) && return_type.descriptor == 'V'
273
- return true
274
- end
275
- false
276
- end
277
-
278
- def constructor?
279
- false
280
- end
281
-
282
- def accept(visitor, expression)
283
- if self.static?
284
- visitor.visitStaticMethodCall(self, expression)
285
- else
286
- visitor.visitMethodCall(self, expression)
287
- end
288
- end
289
-
290
- def kind
291
- if self.static?
292
- Java::OrgMirahJvmTypes::MemberKind::STATIC_METHOD
293
- else
294
- Java::OrgMirahJvmTypes::MemberKind::METHOD
295
- end
296
- end
297
-
298
- def call(compiler, ast, expression, parameters=nil)
299
- target = compiler.inferred_type(ast.target)
300
- compiler.visit(ast.target, true)
301
-
302
- # if expression, void methods return the called object,
303
- # for consistency and chaining
304
- # TODO: inference phase needs to track that signature is
305
- # void but actual type is callee
306
- if expression && void?
307
- compiler.method.dup
308
- end
309
-
310
- parameters ||= ast.parameters
311
- convert_args(compiler, parameters)
312
- if target.interface?
313
- compiler.method.invokeinterface(
314
- target,
315
- name,
316
- [@member.return_type, *@member.argument_types])
317
- else
318
- compiler.method.invokevirtual(
319
- target,
320
- name,
321
- [@member.return_type, *@member.argument_types])
322
- end
323
-
324
- if expression && !void?
325
- # Insert a cast if the inferred type and actual type differ. This is part of generics support.
326
- inferred_return_type = compiler.inferred_type(ast)
327
- if !inferred_return_type.assignableFrom(return_type)
328
- compiler.method.checkcast(inferred_return_type)
329
- end
330
- end
331
-
332
- unless expression || void?
333
- return_type.pop(compiler.method)
334
- end
335
- end
336
-
337
- def call_special(compiler, target, target_type, parameters, expression)
338
- target_type ||= compiler.inferred_type(target)
339
- compiler.visit(target, true)
340
-
341
- # if expression, void methods return the called object,
342
- # for consistency and chaining
343
- # TODO: inference phase needs to track that signature is
344
- # void but actual type is callee
345
- if expression && void?
346
- compiler.method.dup
347
- end
348
-
349
- convert_args(compiler, parameters)
350
- if target_type.interface?
351
- raise "interfaces should not receive call_special"
352
- else
353
- compiler.method.invokespecial(
354
- target_type,
355
- name,
356
- [@member.return_type, *@member.argument_types])
357
- end
358
-
359
- unless expression || void?
360
- return_type.pop(compiler.method)
361
- end
362
- end
363
- end
364
-
365
- class JavaStaticMethod < JavaMethod
366
- def call(compiler, ast, expression, parameters=nil)
367
- target = declaring_class
368
- parameters ||= ast.parameters
369
- convert_args(compiler, parameters)
370
- compiler.method.invokestatic(
371
- target,
372
- name,
373
- [@member.return_type, *@member.argument_types])
374
- # if expression, void static methods return null, for consistency
375
- # TODO: inference phase needs to track that signature is void
376
- # but actual type is null object
377
- compiler.method.aconst_null if expression && void?
378
- return_type.pop(compiler.method) unless expression || void?
379
- end
380
-
381
- def accept(visitor, expression)
382
- visitor.visitStaticMethodCall(self, expression)
383
- end
384
-
385
- def kind
386
- Java::OrgMirahJvmTypes::MemberKind::STATIC_METHOD
387
- end
388
- end
389
-
390
- class JavaFieldAccessor < JavaMethod
391
- def field?
392
- true
393
- end
394
-
395
- def return_type
396
- @types.type(nil, @member.type)
397
- end
398
-
399
- def public?
400
- @member.public?
401
- end
402
-
403
- def final?
404
- @member.final?
405
- end
406
- end
407
-
408
- class JavaFieldGetter < JavaFieldAccessor
409
- def argument_types
410
- []
411
- end
412
-
413
- def call(compiler, ast, expression, parameters=nil)
414
- target = compiler.inferred_type(ast.target)
415
-
416
- # TODO: assert that no args are being passed, though that should have failed lookup
417
-
418
- if expression
419
- if @member.static?
420
- compiler.method.getstatic(target, name, @member.type)
421
- else
422
- compiler.visit(ast.target, true)
423
- compiler.method.getfield(target, name, @member.type)
424
- end
425
- end
426
- end
427
-
428
- def accept(visitor, expression)
429
- if self.static?
430
- visitor.visitStaticFieldAccess(self, expression)
431
- else
432
- visitor.visitFieldAccess(self, expression)
433
- end
434
- end
435
-
436
- def kind
437
- if self.static?
438
- Java::OrgMirahJvmTypes::MemberKind::STATIC_FIELD_ACCESS
439
- else
440
- Java::OrgMirahJvmTypes::MemberKind::FIELD_ACCESS
441
- end
442
- end
443
- end
444
-
445
- class JavaFieldSetter < JavaFieldAccessor
446
- def return_type
447
- @types.type(nil, @member.type)
448
- end
449
-
450
- def argument_types
451
- [@types.type(nil, @member.type)]
452
- end
453
-
454
- def call(compiler, ast, expression, parameters=nil)
455
- target = compiler.inferred_type(ast.target)
456
-
457
- # TODO: assert that no args are being passed, though that should have failed lookup
458
-
459
- parameters ||= ast.parameters
460
- if @member.static?
461
- convert_args(compiler, parameters)
462
- compiler.method.dup if expression
463
- compiler.method.putstatic(target, name, @member.type)
464
- else
465
- compiler.visit(ast.target, true)
466
- convert_args(compiler, parameters)
467
- compiler.method.dup_x2 if expression
468
- compiler.method.putfield(target, name, @member.type)
469
- end
470
- end
471
-
472
- def accept(visitor, expression)
473
- if self.static?
474
- visitor.visitStaticFieldAssign(self, expression)
475
- else
476
- visitor.visitFieldAssign(self, expression)
477
- end
478
- end
479
-
480
- def kind
481
- if self.static?
482
- Java::OrgMirahJvmTypes::MemberKind::STATIC_FIELD_ASSIGN
483
- else
484
- Java::OrgMirahJvmTypes::MemberKind::FIELD_ASSIGN
485
- end
486
- end
487
- end
488
-
489
- class MirahMember
490
- attr_reader :name, :argument_types, :declaring_class, :return_type
491
- attr_reader :exception_types
492
-
493
- def initialize(klass, name, args, return_type, static, exceptions)
494
- if return_type.name == 'void' || return_type.name == ':unreachable'
495
- return_type = nil
496
- end
497
- @declaring_class = klass
498
- @name = name
499
- @argument_types = args
500
- @return_type = return_type
501
- @static = static
502
- @exception_types = exceptions || []
503
- end
504
-
505
- def static?
506
- @static
507
- end
508
-
509
- def abstract?
510
- @declaring_class.interface?
511
- end
512
-
513
- def varargs?
514
- false
515
- end
516
-
517
- def isVararg
518
- varargs?
519
- end
520
- end
521
-
522
- class Type
523
- java_import "org.mirah.jvm.types.JVMMethod" rescue nil
524
-
525
- def method_listeners
526
- if meta?
527
- unmeta.method_listeners
528
- else
529
- @method_listeners ||= {}
530
- end
531
- end
532
-
533
- def method_updated(name)
534
- listeners = method_listeners[name]
535
- listeners.values.each do |l|
536
- if l.kind_of?(Proc)
537
- l.call(name)
538
- else
539
- l.method_updated(name)
540
- end
541
- end if listeners
542
- end
543
-
544
- def add_method_listener(name, listener=nil, &block)
545
- listeners = method_listeners[name] ||= {}
546
- if listener
547
- unless listener.respond_to?(:method_updated) || listener.kind_of?(Proc)
548
- raise "Invalid listener"
549
- end
550
- listeners[listener] = listener
551
- else
552
- listeners[block] = block
553
- end
554
- if !self.meta? && jvm_type && super_class_valid?
555
- superclass.add_method_listener(name, self)
556
- end
557
- interfaces.each {|i| i.add_method_listener(name, self) unless i.isError}
558
- end
559
-
560
- # TODO take a scope and check visibility
561
- def find_callable_macros(name)
562
- macros = find_callable_macros2 name
563
- macros.concat collect_up_interface_tree {|interface| interface.declared_macros(name) }
564
- macros
565
- end
566
-
567
- def find_callable_macros2(name)
568
- collect_up_inheritance_tree {|type| type.declared_macros name }
569
- end
570
-
571
- def find_callable_static_methods(name)
572
- collect_up_inheritance_tree { |type| type.declared_class_methods(name) }
573
- end
574
-
575
- # TODO take a scope and check visibility
576
- def find_callable_methods(name, include_interfaces=false, &proc)
577
- if block_given?
578
- add_method_listener(name) {proc.call(find_callable_methods(name))}
579
- proc.call(find_callable_methods(name))
580
- return
581
- end
582
-
583
- methods = find_callable_methods2 name
584
-
585
- if self.interface? || include_interfaces # TODO || self.abstract?
586
- methods.concat collect_up_interface_tree { |interface| interface.declared_instance_methods(name) }
587
- end
588
- methods
589
- end
590
-
591
- def find_callable_methods2(name)
592
- collect_up_inheritance_tree { |type| type.declared_instance_methods(name) }
593
- end
594
-
595
- def get_method(name, args)
596
- method = find_method(self, name, args, nil, meta?)
597
- unless method
598
- # Allow constant narrowing for assignment methods
599
- if name =~ /=$/ && args[-1].respond_to?(:narrow!)
600
- if args[-1].narrow!
601
- method = find_method(self, name, args, meta?)
602
- end
603
- end
604
- end
605
- method
606
- end
607
-
608
- def constructor(*types)
609
- begin
610
- constructor = jvm_type.getConstructor(*bitescript_signatures(types))
611
- return JavaConstructor.new(@type_system, constructor) if constructor
612
- rescue => ex
613
- log("#{ex.message}\n#{ex.backtrace.join("\n")}")
614
- end
615
- raise NameError, "No constructor #{name}(#{types.join ', '})"
616
- end
617
-
618
- def java_method(name, *types)
619
- intrinsic = intrinsics[name][types]
620
- return intrinsic if intrinsic
621
-
622
- begin
623
- method = jvm_type.getDeclaredMethod(name, *bitescript_signatures(types)) if jvm_type
624
-
625
- if method.nil? && superclass
626
- method = superclass.java_method(name, *types) rescue nil
627
- end
628
-
629
- if method.nil? && jvm_type && jvm_type.abstract?
630
- interfaces.each do |interface|
631
- method = interface.java_method(name, *types) rescue nil
632
- break if method
633
- end
634
- end
635
-
636
- return method if method.kind_of?(JavaCallable)
637
- if method && method.static? == meta?
638
- return wrap_jvm_method method
639
- end
640
- rescue => ex
641
- log("#{ex.message}\n#{ex.backtrace.join("\n")}")
642
- end
643
- raise NameError, "No method #{self.name}.#{name}(#{types.join ', '})"
644
- end
645
-
646
- def declared_instance_methods(name=nil)
647
- all_declared_jvm_methods(name).reject{ |method| method.static? && !method.synthetic? } +
648
- (meta? ? unmeta : self).declared_intrinsics(name)
649
- end
650
-
651
- def declared_class_methods(name=nil)
652
- all_declared_jvm_methods(name).select{ |method| method.static? && !method.synthetic? } +
653
- meta.declared_intrinsics(name)
654
- end
655
-
656
- def declared_constructors
657
- jvm_type.getConstructors.map do |method|
658
- JavaConstructor.new(@type_system, method)
659
- end
660
- end
661
-
662
- def field_getter(name)
663
- field = jvm_field(name)
664
- return nil unless field
665
-
666
- JavaFieldGetter.new(@type_system, field)
667
- end
668
-
669
- def field_setter(name)
670
- field = jvm_field(name)
671
- return nil unless field
672
-
673
- JavaFieldSetter.new(@type_system, field)
674
- end
675
-
676
- def inner_class_getter(name)
677
- full_name = "#{self.name}$#{name}"
678
- inner_class = @type_system.type(nil, full_name) rescue nil
679
- return unless inner_class
680
-
681
- inner_class.inner_class = true
682
- macro = Macro.new(self, name, []) do |call, typer|
683
- Mirah::AST::Constant.new(call.position, Mirah::AST::SimpleString.new(call.position, full_name))
684
- end
685
- intrinsics[name][[]] = macro
686
- end
687
-
688
- def getDeclaredFields
689
- @member.getDeclaredFields.to_java(JVMMethod)
690
- end
691
-
692
- def getDeclaredField(name)
693
- @member.getDeclaredField(name)
694
- end
695
-
696
- def hasStaticField(name)
697
- f = getDeclaredField(name)
698
- f && f.static?
699
- end
700
-
701
- protected
702
-
703
- def find_interfaces
704
- collect_up_inheritance_tree {|type| type.interfaces }
705
- end
706
-
707
- def collect_up_interface_tree &block
708
- interfaces = find_interfaces
709
- things = []
710
- seen = {}
711
- until interfaces.empty?
712
- interface = interfaces.pop
713
- next if seen[interface]
714
- next if interface.isError
715
- seen[interface] = true
716
- interfaces.concat(interface.interfaces)
717
- new_things = block.call interface
718
- things.concat new_things if new_things
719
- end
720
- things
721
- end
722
-
723
- def collect_up_inheritance_tree(&block)
724
- things = []
725
- new_things = block.call(self)
726
- things.concat new_things if new_things
727
- if super_class_valid?
728
- things.concat superclass.collect_up_inheritance_tree(&block)
729
- end
730
- things
731
- end
732
-
733
- private
734
-
735
- def super_class_valid?
736
- superclass && !superclass.error?
737
- end
738
-
739
- def all_declared_jvm_methods(name=nil)
740
- return [] if !jvm_type || (meta? ? unmeta : self).array?
741
-
742
- jvm_type.getDeclaredMethods(name).map { |method| wrap_jvm_method method }
743
- end
744
-
745
- def wrap_jvm_method method
746
- if (method.static? && !method.synthetic?)
747
- JavaStaticMethod.new(@type_system, method)
748
- else
749
- JavaMethod.new(@type_system, method)
750
- end
751
- end
752
-
753
- def jvm_field(name)
754
- return nil unless jvm_type
755
- field = jvm_type.getField(name)
756
- field ||= begin
757
- ifaces = jvm_type.interfaces.map { |i| wrap_with_mirror i }
758
- ifaces.map{|i| i.getField(name) }.compact.first
759
- end
760
- field ||= begin
761
- mirror = wrap_with_mirror jvm_type.superclass
762
- potential, mirror = [mirror.getField(name), wrap_with_mirror(mirror.superclass)] until potential || mirror.nil?
763
- potential
764
- end
765
- field
766
- end
767
-
768
- def wrap_with_mirror type
769
- return unless type
770
- if type.respond_to?(:getField)
771
- type
772
- else
773
- BiteScript::ASM::ClassMirror.for_name type.class_name
774
- end
775
- end
776
-
777
- def bitescript_signatures types
778
- types.map {|type| BiteScript::Signature.class_id(type)}
779
- end
780
- end
781
-
782
- class TypeDefinition
783
- java_import "org.mirah.jvm.types.JVMMethod" rescue nil
784
-
785
- def java_method(name, *types)
786
- method = first_matching instance_methods[name], types
787
- return method if method
788
- intrinsic = intrinsics[name][types]
789
- return intrinsic if intrinsic
790
- raise NameError, "No method #{self.name}.#{name}(#{types.join ', '})"
791
- end
792
-
793
- def java_static_method(name, *types)
794
- method = first_matching static_methods[name], types
795
- return method if method
796
- intrinsic = meta.intrinsics[name][types]
797
- return intrinsic if intrinsic
798
- raise NameError, "No method #{self.name}.#{name}(#{types.join ', '})"
799
- end
800
-
801
- def constructor(*types)
802
- constructor = first_matching constructors, types
803
- return constructor if constructor
804
- raise NameError, "No constructor #{name}(#{types.join ', '})"
805
- end
806
-
807
- def declared_instance_methods(name=nil)
808
- declared_methods self, instance_methods, name
809
- end
810
-
811
- def declared_class_methods(name=nil)
812
- declared_methods meta, static_methods, name
813
- end
814
-
815
- def constructors
816
- if @constructors.nil?
817
- @constructors = []
818
- declare_method('initialize', [], self, [])
819
- @have_default_constructor = true
820
- end
821
- @constructors
822
- end
823
- alias :declared_constructors :constructors
824
-
825
- def instance_methods
826
- @instance_methods ||= Hash.new {|h, k| h[k] = []}
827
- end
828
-
829
- def static_methods
830
- @static_methods ||= Hash.new {|h, k| h[k] = []}
831
- end
832
-
833
- def getDeclaredFields
834
- @fields.values.to_java(JVMMethod)
835
- end
836
-
837
- def getDeclaredField(name)
838
- @fields[name]
839
- end
840
-
841
- def hasStaticField(name)
842
- f = getDeclaredField(name)
843
- f && f.static?
844
- end
845
-
846
- def declared_fields
847
- @fields ||= {}
848
- end
849
-
850
- def declare_field(name, type, static)
851
- if type.isError
852
- declared_fields.delete(name)
853
- return
854
- end
855
- declared_fields[name] ||= MirahMember.new(self, name, [], type, static, [])
856
- end
857
-
858
- def declare_method(name, arguments, type, exceptions)
859
- raise "Bad args" unless arguments.all?
860
- if type.isError
861
- instance_methods.delete(name)
862
- method_updated(name)
863
- return
864
- end
865
- member = MirahMember.new(self, name, arguments, type, false, exceptions)
866
- if name == 'initialize'
867
- # The ordering is important here:
868
- # The first call to constructors initializes @have_default_constructor.
869
- if constructors.size == 1 && @have_default_constructor
870
- constructors.clear
871
- @have_default_constructor = false
872
- elsif constructors.size > 1 && @have_default_constructor
873
- raise "Invalid state: default constructor but #{constructors.size} constructors"
874
- end
875
- constructors << JavaConstructor.new(@type_system, member)
876
- else
877
- instance_methods[name] << JavaMethod.new(@type_system, member)
878
- end
879
- method_updated(name)
880
- end
881
-
882
- def declare_static_method(name, arguments, type, exceptions)
883
- if type.isError
884
- static_methods.delete(name)
885
- else
886
- member = MirahMember.new(self, name, arguments, type, true, exceptions)
887
- static_methods[name] << JavaStaticMethod.new(@type_system, member)
888
- end
889
- method_updated(name)
890
- end
891
-
892
- def interface?
893
- false
894
- end
895
-
896
- def field_getter(name)
897
- nil
898
- end
899
-
900
- def field_setter(name)
901
- nil
902
- end
903
-
904
- private
905
-
906
- def declared_methods target, method_hash, name
907
- # should the declared intrinsics be first here? nh
908
- target.declared_intrinsics(name) +
909
- if name.nil?
910
- method_hash.values.flatten
911
- else
912
- method_hash[name]
913
- end
914
- end
915
-
916
- def first_matching method_list, types
917
- method_list.find {|m| m.argument_types == types}
918
- end
919
- end
920
-
921
- class TypeDefMeta
922
- def constructor(*args)
923
- unmeta.constructor(*args)
924
- end
925
-
926
- def java_method(*args)
927
- unmeta.java_static_method(*args)
928
- end
929
-
930
- def declared_class_methods(name=nil)
931
- unmeta.declared_class_methods(name)
932
- end
933
-
934
- def declared_instance_methods(name=nil)
935
- unmeta.declared_instance_methods(name)
936
- end
937
-
938
- def field_getter(name)
939
- nil
940
- end
941
-
942
- def field_setter(name)
943
- nil
944
- end
945
- end
946
- end