mirah 0.0.12-java → 0.1.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. data/History.txt +372 -0
  2. data/README.txt +4 -5
  3. data/Rakefile +178 -55
  4. data/examples/appengine/Readme +3 -3
  5. data/examples/appengine/src/org/mirah/MirahApp.mirah +1 -1
  6. data/examples/appengine/src/org/mirah/list.dhtml +1 -1
  7. data/examples/bintrees.mirah +1 -1
  8. data/examples/edb.mirah +1 -1
  9. data/examples/fib.mirah +1 -1
  10. data/examples/interfaces.mirah +1 -1
  11. data/examples/macros/{string-each-char.mirah → string_each_char.mirah} +4 -5
  12. data/examples/maven/README.txt +1 -1
  13. data/examples/maven/src/main/mirah/hello_mirah.mirah +1 -1
  14. data/examples/plugins/appengine/Rakefile +1 -1
  15. data/examples/plugins/appengine/src/com/google/appengine/ext/duby/db/MetaModel.mirah +1 -1
  16. data/examples/plugins/appengine/src/com/google/appengine/ext/duby/db/Model.duby +1 -1
  17. data/examples/plugins/appengine/test/com/google/appengine/ext/duby/db/ModelTest.duby +1 -1
  18. data/examples/rosettacode/100-doors.mirah +6 -6
  19. data/examples/rosettacode/README.txt +3 -3
  20. data/examples/rosettacode/boolean-values.mirah +1 -1
  21. data/examples/rosettacode/comments.mirah +1 -1
  22. data/examples/rosettacode/count-occurrences-of-a-substring.mirah +1 -1
  23. data/examples/rosettacode/factorial.mirah +1 -1
  24. data/examples/rosettacode/fibonacci.mirah +1 -1
  25. data/examples/rosettacode/fizz-buzz.mirah +2 -2
  26. data/examples/rosettacode/flatten-a-list.mirah +4 -4
  27. data/examples/rosettacode/guess-the-number.mirah +2 -2
  28. data/examples/rosettacode/hamming-numbers.mirah +4 -4
  29. data/examples/rosettacode/is-string-numeric.mirah +22 -22
  30. data/examples/rosettacode/palindrome.mirah +2 -2
  31. data/examples/rosettacode/random-numbers.mirah +1 -1
  32. data/examples/rosettacode/repeat-a-string.mirah +1 -1
  33. data/examples/rosettacode/reverse-a-string.mirah +1 -1
  34. data/examples/rosettacode/rot-13.mirah +5 -5
  35. data/examples/rosettacode/secure-temporary-file.mirah +2 -2
  36. data/examples/rosettacode/sleep.mirah +1 -1
  37. data/examples/rosettacode/string-length.mirah +5 -5
  38. data/examples/swing.mirah +1 -1
  39. data/examples/test.edb +1 -1
  40. data/javalib/mirah-bootstrap.jar +0 -0
  41. data/javalib/mirah-builtins.jar +0 -0
  42. data/javalib/mirah-parser.jar +0 -0
  43. data/javalib/mirah-util.jar +0 -0
  44. data/lib/duby.rb +1 -1
  45. data/lib/mirah.rb +50 -28
  46. data/lib/mirah/ast.rb +15 -605
  47. data/lib/mirah/ast/scope.rb +98 -69
  48. data/lib/mirah/commands.rb +1 -1
  49. data/lib/mirah/commands/base.rb +7 -7
  50. data/lib/mirah/commands/compile.rb +3 -3
  51. data/lib/mirah/commands/parse.rb +7 -5
  52. data/lib/mirah/commands/run.rb +12 -19
  53. data/lib/mirah/compiler.rb +15 -23
  54. data/lib/mirah/errors.rb +16 -1
  55. data/lib/mirah/generator.rb +79 -39
  56. data/lib/mirah/jvm/compiler.rb +1 -19
  57. data/lib/mirah/jvm/compiler/base.rb +233 -90
  58. data/lib/mirah/jvm/compiler/jvm_bytecode.rb +675 -363
  59. data/lib/mirah/jvm/method_lookup.rb +134 -65
  60. data/lib/mirah/jvm/typer.rb +10 -5
  61. data/lib/mirah/jvm/types.rb +10 -2
  62. data/lib/mirah/jvm/types/array_type.rb +10 -12
  63. data/lib/mirah/{compiler/type.rb → jvm/types/ast_ext.rb} +12 -8
  64. data/lib/mirah/jvm/types/basic_types.rb +26 -33
  65. data/lib/mirah/jvm/types/bitescript_ext.rb +1 -1
  66. data/lib/mirah/jvm/types/block_type.rb +15 -0
  67. data/lib/mirah/jvm/types/boolean.rb +8 -4
  68. data/lib/mirah/jvm/types/dynamic_type.rb +12 -13
  69. data/lib/mirah/jvm/types/enumerable.rb +7 -7
  70. data/lib/mirah/jvm/types/extensions.rb +11 -6
  71. data/lib/mirah/jvm/types/factory.rb +624 -94
  72. data/lib/mirah/jvm/types/floats.rb +21 -15
  73. data/lib/mirah/jvm/types/generic_type.rb +72 -0
  74. data/lib/mirah/jvm/types/implicit_nil_type.rb +29 -0
  75. data/lib/mirah/jvm/types/integers.rb +26 -71
  76. data/lib/mirah/jvm/types/interface_definition.rb +3 -3
  77. data/lib/mirah/jvm/types/intrinsics.rb +203 -168
  78. data/lib/mirah/jvm/types/literals.rb +6 -6
  79. data/lib/mirah/jvm/types/meta_type.rb +13 -4
  80. data/lib/mirah/jvm/types/methods.rb +281 -93
  81. data/lib/mirah/jvm/types/null_type.rb +17 -5
  82. data/lib/mirah/jvm/types/number.rb +10 -7
  83. data/lib/mirah/jvm/types/primitive_type.rb +17 -6
  84. data/lib/mirah/jvm/types/source_mirror.rb +12 -7
  85. data/lib/mirah/jvm/types/type.rb +107 -23
  86. data/lib/mirah/jvm/types/type_definition.rb +25 -10
  87. data/lib/mirah/jvm/types/unreachable_type.rb +1 -1
  88. data/lib/mirah/jvm/types/void_type.rb +3 -3
  89. data/lib/mirah/parser.rb +154 -16
  90. data/lib/mirah/plugin/edb.rb +1 -1
  91. data/lib/mirah/transform.rb +1 -2
  92. data/lib/mirah/transform/ast_ext.rb +24 -43
  93. data/lib/mirah/transform/transformer.rb +29 -224
  94. data/lib/mirah/typer.rb +2 -16
  95. data/lib/mirah/util/argument_processor.rb +25 -10
  96. data/lib/mirah/util/class_loader.rb +1 -1
  97. data/lib/mirah/util/compilation_state.rb +16 -17
  98. data/lib/mirah/util/delegate.rb +2 -2
  99. data/lib/mirah/util/logging.rb +110 -0
  100. data/lib/mirah/util/process_errors.rb +69 -11
  101. data/lib/mirah/version.rb +1 -1
  102. data/test/core/commands_test.rb +6 -24
  103. data/test/core/env_test.rb +5 -5
  104. data/{lib/mirah/jvm/source_generator/typer.rb → test/core/generator_test.rb} +9 -9
  105. data/test/core/typer_test.rb +196 -158
  106. data/test/core/util/argument_processor_test.rb +10 -10
  107. data/test/core/util/class_loader_test.rb +6 -5
  108. data/test/fixtures/org/foo/LowerCaseInnerClass$inner.class +0 -0
  109. data/test/fixtures/org/foo/LowerCaseInnerClass.class +0 -0
  110. data/test/fixtures/org/foo/LowerCaseInnerClass.java +7 -0
  111. data/test/jvm/annotations_test.rb +5 -5
  112. data/test/jvm/blocks_test.rb +140 -88
  113. data/test/jvm/bytecode_test_helper.rb +112 -94
  114. data/test/jvm/cast_test.rb +162 -0
  115. data/test/jvm/constructors_test.rb +18 -8
  116. data/test/jvm/enumerable_test.rb +77 -44
  117. data/test/jvm/example_test.rb +53 -0
  118. data/test/jvm/factory_test.rb +7 -1
  119. data/test/jvm/generics_test.rb +57 -0
  120. data/test/jvm/hash_test.rb +106 -0
  121. data/test/jvm/import_test.rb +81 -0
  122. data/test/jvm/interface_test.rb +73 -0
  123. data/test/jvm/java_typer_test.rb +92 -66
  124. data/{lib/mirah/typer/base.rb → test/jvm/jvm_commands_test.rb} +6 -10
  125. data/test/jvm/jvm_compiler_test.rb +170 -604
  126. data/test/jvm/list_extensions_test.rb +23 -0
  127. data/test/jvm/macros_test.rb +197 -32
  128. data/test/jvm/main_method_test.rb +4 -4
  129. data/test/jvm/numeric_extensions_test.rb +13 -0
  130. data/test/jvm/rescue_test.rb +73 -16
  131. data/test/jvm/varargs_test.rb +65 -0
  132. data/test/test_helper.rb +1 -2
  133. metadata +234 -251
  134. data/examples/SortClosure$__xform_tmp_1.class +0 -0
  135. data/examples/SortClosure$__xform_tmp_2.class +0 -0
  136. data/examples/SortClosure.class +0 -0
  137. data/examples/macros/StringEachChar$Extension1.class +0 -0
  138. data/lib/mirah/ast/call.rb +0 -345
  139. data/lib/mirah/ast/class.rb +0 -359
  140. data/lib/mirah/ast/flow.rb +0 -381
  141. data/lib/mirah/ast/intrinsics.rb +0 -563
  142. data/lib/mirah/ast/literal.rb +0 -178
  143. data/lib/mirah/ast/local.rb +0 -112
  144. data/lib/mirah/ast/method.rb +0 -408
  145. data/lib/mirah/ast/structure.rb +0 -387
  146. data/lib/mirah/ast/type.rb +0 -146
  147. data/lib/mirah/commands/base.rb~ +0 -57
  148. data/lib/mirah/compiler/call.rb +0 -45
  149. data/lib/mirah/compiler/class.rb +0 -81
  150. data/lib/mirah/compiler/flow.rb +0 -109
  151. data/lib/mirah/compiler/literal.rb +0 -130
  152. data/lib/mirah/compiler/local.rb +0 -59
  153. data/lib/mirah/compiler/method.rb +0 -44
  154. data/lib/mirah/compiler/structure.rb +0 -65
  155. data/lib/mirah/jvm/compiler/java_source.rb +0 -787
  156. data/lib/mirah/jvm/method_lookup.rb~ +0 -247
  157. data/lib/mirah/jvm/source_generator/builder.rb +0 -468
  158. data/lib/mirah/jvm/source_generator/loops.rb +0 -131
  159. data/lib/mirah/jvm/source_generator/precompile.rb +0 -210
  160. data/lib/mirah/plugin/gwt.rb +0 -189
  161. data/lib/mirah/plugin/java.rb +0 -70
  162. data/lib/mirah/transform/error.rb +0 -13
  163. data/lib/mirah/transform/helper.rb +0 -765
  164. data/lib/mirah/typer/simple.rb +0 -384
  165. data/lib/mirah/version.rb~ +0 -18
  166. data/test/core/ast_test.rb +0 -382
  167. data/test/core/compilation_test.rb +0 -130
  168. data/test/core/macros_test.rb +0 -61
  169. data/test/jvm/javac_test_helper.rb +0 -89
  170. data/test/jvm/jvm_compiler_test.rb~ +0 -2181
  171. data/test/plugins/gwt_test.rb +0 -69
@@ -1,178 +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
- module Mirah::AST
17
- class Array < Node
18
- def initialize(parent, line_number, &block)
19
- super(parent, line_number, &block)
20
- end
21
-
22
- def infer(typer, expression)
23
- resolve_if(typer) do
24
- children.each do |kid|
25
- kid.infer(typer, true)
26
- end
27
- @inferred_type = typer.array_type
28
- end
29
- end
30
- end
31
-
32
- class Fixnum < Node
33
- include Literal
34
-
35
- def initialize(parent, line_number, literal)
36
- super(parent, line_number)
37
- @literal = literal
38
- end
39
-
40
- def infer(typer, expression)
41
- resolve_if(typer) {@inferred_type = typer.fixnum_type(@literal)}
42
- end
43
-
44
- def ==(other)
45
- @literal == other.literal
46
- end
47
-
48
- def eql?(other)
49
- self.class == other.class && @literal.eql?(other.literal)
50
- end
51
- end
52
-
53
- class Float < Node
54
- include Literal
55
-
56
- def initialize(parent, line_number, literal)
57
- super(parent, line_number)
58
- @literal = literal
59
- end
60
-
61
- def infer(typer, expression)
62
- resolve_if(typer) {@inferred_type = typer.float_type(@literal)}
63
- end
64
- end
65
-
66
- class Hash < Node; end
67
-
68
- class Regexp < Node
69
- include Literal
70
-
71
- def initialize(parent, line_number, literal)
72
- super(parent, line_number)
73
- @literal = literal
74
- end
75
-
76
- def infer(typer, expression)
77
- return @inferred_type if resolved?
78
- resolved!
79
- @inferred_type ||= typer.regexp_type
80
- end
81
- end
82
-
83
- class String < Node
84
- include Literal
85
- include Scoped
86
- include Java::DubyLangCompiler.StringNode
87
-
88
- def initialize(parent, line_number, literal)
89
- super(parent, line_number)
90
- @literal = literal
91
- end
92
-
93
- def infer(typer, expression)
94
- return @inferred_type if resolved?
95
- resolved!
96
- @inferred_type ||= typer.string_type
97
- end
98
-
99
- def type_reference(typer)
100
- typer.type_reference(scope, @literal)
101
- end
102
-
103
- def toString
104
- @literal
105
- end
106
- end
107
-
108
- class StringConcat < Node
109
- def initialize(parent, position, &block)
110
- super(parent, position, &block)
111
- end
112
-
113
- def infer(typer, expression)
114
- unless resolved?
115
- resolved = true
116
- children.each {|node| node.infer(typer, true); resolved &&= node.resolved?}
117
- resolved! if resolved
118
- @inferred_type ||= typer.string_type
119
- end
120
- @inferred_type
121
- end
122
- end
123
-
124
- class ToString < Node
125
- child :body
126
-
127
- def initialize(parent, position)
128
- super(parent, position)
129
- end
130
-
131
- def infer(typer, expression)
132
- unless resolved?
133
- body.infer(typer, true)
134
- resolved! if body.resolved?
135
- @inferred_type ||= typer.string_type
136
- end
137
- @inferred_type
138
- end
139
- end
140
-
141
- class Symbol < Node; end
142
-
143
- class Boolean < Node
144
- include Literal
145
-
146
- def initialize(parent, line_number, literal)
147
- super(parent, line_number)
148
- @literal = literal
149
- end
150
-
151
- def infer(typer, expression)
152
- return @inferred_type if resolved?
153
- resolved!
154
- @inferred_type ||= typer.boolean_type
155
- end
156
- end
157
-
158
- class Null < Node
159
- include Literal
160
-
161
- def initialize(parent, line_number)
162
- super(parent, line_number)
163
- end
164
-
165
- def infer(typer, expression)
166
- return @inferred_type if resolved?
167
- resolved!
168
- @inferred_type ||= typer.null_type
169
- end
170
- end
171
-
172
- class Self < Node
173
- include Scoped
174
- def infer(typer, expression)
175
- @inferred_type ||= scope.static_scope.self_type
176
- end
177
- end
178
- end
@@ -1,112 +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
- module Mirah::AST
17
- class LocalDeclaration < Node
18
- include Named
19
- include Typed
20
- include Scoped
21
-
22
- child :type_node
23
- attr_accessor :type
24
-
25
- def initialize(parent, line_number, name, &block)
26
- super(parent, line_number, &block)
27
- self.name = name
28
- end
29
-
30
- def captured?
31
- scope.static_scope.captured?(name)
32
- end
33
-
34
- def infer(typer, expression)
35
- resolve_if(typer) do
36
- scope.static_scope << name
37
- @type = type_node.type_reference(typer)
38
- end
39
- end
40
-
41
- def resolved!(typer)
42
- typer.learn_local_type(containing_scope, name, @inferred_type)
43
- super
44
- end
45
- end
46
-
47
- class LocalAssignment < Node
48
- include Named
49
- include Valued
50
- include Scoped
51
-
52
- child :value
53
-
54
- def initialize(parent, line_number, name, &block)
55
- super(parent, line_number, &block)
56
- self.name = name
57
- end
58
-
59
- def captured?
60
- scope.static_scope.captured?(name)
61
- end
62
-
63
- def to_s
64
- "LocalAssignment(name = #{name}, scope = #{scope}, captured = #{captured? == true})"
65
- end
66
-
67
- def infer(typer, expression)
68
- resolve_if(typer) do
69
- scope.static_scope << name
70
- type = typer.infer(value, true)
71
- if type && type.null?
72
- type = typer.local_type(containing_scope, name) unless typer.last_chance
73
- end
74
- type
75
- end
76
- end
77
-
78
- def resolved!(typer)
79
- typer.learn_local_type(containing_scope, name, @inferred_type)
80
- super
81
- end
82
- end
83
-
84
- class Local < Node
85
- include Named
86
- include Scoped
87
-
88
- def initialize(parent, line_number, name)
89
- super(parent, line_number, [])
90
- self.name = name
91
- end
92
-
93
- def captured?
94
- scope && scope.static_scope.captured?(name)
95
- end
96
-
97
- def to_s
98
- "Local(name = #{name}, scope = #{scope}, captured = #{captured? == true})"
99
- end
100
-
101
- def type_reference(typer)
102
- typer.type_reference(scope, @name)
103
- end
104
-
105
- def infer(typer, expression)
106
- resolve_if(typer) do
107
- scope.static_scope << name
108
- typer.local_type(containing_scope, name)
109
- end
110
- end
111
- end
112
- end
@@ -1,408 +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
- module Mirah::AST
17
- class Arguments < Node
18
- child :required
19
- child :opt_args
20
- child :rest_arg
21
- child :required2
22
- child :block_arg
23
-
24
- def initialize(parent, line_number, &block)
25
- super(parent, line_number, &block)
26
- end
27
-
28
- def infer(typer, expression)
29
- unless resolved?
30
- @inferred_type = args ? args.map {|arg| typer.infer(arg, true)} : []
31
- if @inferred_type.all?
32
- resolved!
33
- else
34
- typer.defer(self)
35
- end
36
- end
37
- @inferred_type
38
- end
39
-
40
- def arg_types_match(arg_node, child_index)
41
- if RequiredArgument == arg_node && (child_index == 0 || child_index == 3)
42
- return true
43
- else
44
- return OptionalArgument == arg_node && child_index == 1
45
- end
46
- end
47
-
48
- def validate_child(args, child_index)
49
- if args.kind_of?(::Array)
50
- args.each_with_index do |arg, arg_index|
51
- if UnquotedValue === arg
52
- actual_arg = arg.f_arg
53
- if arg_types_match(actual_arg, child_index)
54
- args[arg_index] = actual_arg
55
- actual_arg.parent = self
56
- else
57
- args[arg_index, 1] = []
58
- merge_args(actual_arg, child_index)
59
- end
60
- end
61
- end
62
- elsif UnquotedValue == args
63
- @children[child_index] = nil
64
- merge_args(args.f_arg, child_index)
65
- end
66
- end
67
-
68
- def merge_args(args, child_index)
69
- args.parent = self if Argument === args
70
- case args
71
- when Arguments
72
- args.children.each_with_index {|child, i| merge_args(child, i)}
73
- when ::Array
74
- args.each {|arg| merge_args(arg, child_index)}
75
- when RequiredArgument
76
- if child_index > 2
77
- self.required2 << args
78
- else
79
- self.required << args
80
- end
81
- when OptionalArgument
82
- self.opt_args << args
83
- when RestArgument
84
- raise "Multiple rest args" unless rest_arg.nil?
85
- self.rest_arg = args
86
- when BlockArgument
87
- raise "Multiple block args" unless block_arg.nil?
88
- self.block_arg = args
89
- else
90
- raise "Unknown argument type #{args.class}"
91
- end
92
- end
93
-
94
- def args
95
- args = (required || []) + (opt_args || [])
96
- args << block_arg if block_arg
97
- return args
98
- end
99
- end
100
-
101
- class Argument < Node
102
- include Typed
103
-
104
- def resolved!(typer)
105
- typer.learn_local_type(containing_scope, name, @inferred_type)
106
- super
107
- end
108
- end
109
-
110
- class RequiredArgument < Argument
111
- include Named
112
- include Scoped
113
- child :type_node
114
-
115
- def initialize(parent, line_number, name, type=nil)
116
- super(parent, line_number, [type])
117
-
118
- self.name = name
119
- end
120
-
121
- def infer(typer, expression)
122
- resolve_if(typer) do
123
- scope.static_scope << name
124
- # if not already typed, check parent of parent (MethodDefinition)
125
- # for signature info
126
- method_def = parent.parent
127
- signature = method_def.signature
128
-
129
- if type_node
130
- if ::String === type_node # How does this happen?
131
- signature[name.intern] = typer.type_reference(scope, type_node)
132
- else
133
- signature[name.intern] = type_node.type_reference(typer)
134
- end
135
- end
136
-
137
- # if signature, search for this argument
138
- signature[name.intern] || typer.local_type(containing_scope, name)
139
- end
140
- end
141
-
142
- def validate_type_node
143
- if UnquotedValue === type_node
144
- self.type_node = type_node.type
145
- end
146
- end
147
- end
148
-
149
- class OptionalArgument < Argument
150
- include Named
151
- include Scoped
152
- child :type_node
153
- child :value
154
-
155
- def initialize(parent, line_number, name, &block)
156
- super(parent, line_number, &block)
157
- self.name = name
158
- end
159
-
160
- def infer(typer, expression)
161
- resolve_if(typer) do
162
- scope.static_scope << name
163
- # if not already typed, check parent of parent (MethodDefinition)
164
- # for signature info
165
- method_def = parent.parent
166
- signature = method_def.signature
167
- value_type = value.infer(typer, true)
168
- declared_type = type_node.type_reference(typer) if type_node
169
- signature[name.intern] = declared_type || value_type
170
- end
171
- end
172
- end
173
-
174
- class RestArgument < Argument
175
- include Named
176
- include Scoped
177
-
178
- def initialize(parent, line_number, name)
179
- super(parent, line_number)
180
-
181
- self.name = name
182
- end
183
-
184
- def infer(typer, expression)
185
- scope.static_scope << name
186
- super
187
- end
188
- end
189
-
190
- class BlockArgument < Argument
191
- include Named
192
- include Scoped
193
- attr_accessor :optional
194
- alias optional? optional
195
-
196
- def initialize(parent, line_number, name)
197
- super(parent, line_number)
198
-
199
- self.name = name
200
- end
201
-
202
- def infer(typer, expression)
203
- scope.static_scope << name
204
- end
205
- end
206
-
207
- class MethodDefinition < Node
208
- include Annotated
209
- include Named
210
- include Scope
211
- include Scoped
212
- include ClassScoped
213
- include Binding
214
- include Java::DubyLangCompiler.MethodDefinition
215
-
216
- child :signature
217
- child :arguments
218
- child :body
219
- # TODO change return_type to a child if we remove the 'returns' macro.
220
- attr_accessor :return_type, :exceptions
221
-
222
- attr_accessor :defining_class
223
- attr_accessor :visibility
224
- attr_accessor :abstract
225
-
226
- def initialize(parent, line_number, name, annotations=[], &block)
227
- @annotations = annotations
228
- super(parent, line_number, &block)
229
- self.name = name
230
- @visibility = (class_scope && class_scope.current_access_level) || :public
231
- end
232
-
233
- def name
234
- super
235
- end
236
-
237
- def infer(typer, expression)
238
- resolve_if(typer) do
239
- @defining_class ||= begin
240
- static_scope.self_node = :self
241
- static_scope.self_type = if static?
242
- scope.static_scope.self_type.meta
243
- else
244
- scope.static_scope.self_type
245
- end
246
- end
247
- @annotations.each {|a| a.infer(typer, true)} if @annotations
248
- typer.infer(arguments, true)
249
- if @return_type
250
- if @return_type.kind_of?(UnquotedValue)
251
- @return_type = @return_type.node
252
- @return_type.parent = self
253
- else
254
- @return_type.parent = self
255
- end
256
- signature[:return] = @return_type.type_reference(typer)
257
- end
258
-
259
- if @exceptions
260
- signature[:throws] = @exceptions.map {|e| e.type_reference(typer)}
261
- end
262
- typer.infer_signature(self)
263
- forced_type = signature[:return]
264
- body_is_expression = (forced_type != typer.no_type)
265
- inferred_type = body ? typer.infer(body, body_is_expression) : typer.no_type
266
-
267
- if inferred_type && arguments.inferred_type.all?
268
- actual_type = if forced_type
269
- forced_type
270
- else
271
- inferred_type
272
- end
273
-
274
- if actual_type.kind_of? Mirah::AST::InlineCode
275
- raise Mirah::Typer::InferenceError.new("Method %s has the same signature as macro of the same name." % name,self)
276
- end
277
-
278
- if actual_type.unreachable?
279
- actual_type = typer.no_type
280
- end
281
-
282
- if !abstract? &&
283
- forced_type != typer.no_type &&
284
- !actual_type.is_parent(inferred_type)
285
- raise Mirah::Typer::InferenceError.new(
286
- "Inferred return type %s is incompatible with declared %s" %
287
- [inferred_type, actual_type], self)
288
- end
289
-
290
- signature[:return] = actual_type
291
- end
292
- end
293
- end
294
-
295
- def resolve_if(typer)
296
- super(typer) do
297
- actual_type = type = yield
298
- argument_types = arguments.inferred_type
299
- # If we know the return type go ahead and tell the typer
300
- # even if we can't infer the body yet.
301
- type ||= signature[:return] if argument_types && argument_types.all?
302
- if type
303
- argument_types ||= [Mirah::AST.error_type] if type.error?
304
- typer.learn_method_type(defining_class, name, argument_types, type, signature[:throws])
305
-
306
- # learn the other overloads as well
307
- args_for_opt = []
308
- if arguments.args
309
- arguments.args.each do |arg|
310
- if OptionalArgument === arg
311
- arg_types_for_opt = args_for_opt.map do |arg_for_opt|
312
- arg_for_opt.infer(typer, true)
313
- end
314
- typer.learn_method_type(defining_class, name, arg_types_for_opt, type, signature[:throws])
315
- end
316
- args_for_opt << arg
317
- end
318
- end
319
- end
320
- actual_type
321
- end
322
- end
323
-
324
- def abstract?
325
- @abstract || InterfaceDeclaration === class_scope
326
- end
327
-
328
- def static?
329
- scope.static_scope.self_type.meta?
330
- end
331
- end
332
-
333
- class StaticMethodDefinition < MethodDefinition
334
- def static?
335
- true
336
- end
337
- end
338
-
339
- class ConstructorDefinition < MethodDefinition
340
- attr_accessor :delegate_args, :calls_super
341
-
342
- def initialize(*args)
343
- super
344
- extract_delegate_constructor
345
- end
346
-
347
- def validate_children
348
- super
349
- if @delegate_args
350
- @delegate_args.each {|arg| arg.parent = self}
351
- end
352
- end
353
-
354
- def first_node
355
- if body.kind_of? Body
356
- body.children[0]
357
- else
358
- body
359
- end
360
- end
361
-
362
- def first_node=(new_node)
363
- if body.kind_of? Body
364
- new_node.parent = body
365
- body.children[0] = new_node
366
- else
367
- self.body = new_node
368
- end
369
- end
370
-
371
- def extract_delegate_constructor
372
- # TODO verify that this constructor exists during type inference.
373
- possible_delegate = first_node
374
- if FunctionalCall === possible_delegate &&
375
- possible_delegate.name == 'initialize'
376
- @delegate_args = possible_delegate.parameters
377
- elsif Super === possible_delegate
378
- @calls_super = true
379
- @delegate_args = possible_delegate.parameters
380
- unless @delegate_args
381
- args = arguments.children.map {|x| x || []}
382
- @delegate_args = args.flatten.map do |arg|
383
- Local.new(self, possible_delegate.position, arg.name)
384
- end
385
- end
386
- end
387
- self.first_node = Noop.new(self, position) if @delegate_args
388
- end
389
-
390
- def infer(typer, expression)
391
- unless @inferred_type
392
- delegate_args.each {|a| typer.infer(a, true)} if delegate_args
393
- end
394
- super
395
- end
396
- end
397
-
398
- defmacro('returns') do |transformer, fcall, parent|
399
- fcall.scope.return_type = fcall.parameters[0]
400
- Noop.new(parent, fcall.position)
401
- end
402
-
403
-
404
- defmacro('throws') do |transformer, fcall, parent|
405
- fcall.scope.exceptions = fcall.parameters
406
- Noop.new(parent, fcall.position)
407
- end
408
- end