Class: Mirah::JVM::Compiler::Base
- Inherits:
-
Object
- Object
- Mirah::JVM::Compiler::Base
- Defined in:
- lib/mirah/plugin/gwt.rb,
lib/mirah/jvm/compiler/base.rb
Direct Known Subclasses
Defined Under Namespace
Classes: CompilationError
Instance Attribute Summary collapse
-
#class ⇒ Object
Returns the value of attribute class.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#method ⇒ Object
Returns the value of attribute method.
-
#static ⇒ Object
Returns the value of attribute static.
Instance Method Summary collapse
- #base_define_method(node, args_are_types) ⇒ Object
- #begin_main ⇒ Object
- #body(body, expression) {|| ... } ⇒ Object
- #compile(ast, expression = false) ⇒ Object
- #compile_self ⇒ Object
- #constructor(node, args_are_types) ⇒ Object
-
#create_method_builder(name, node, static, exceptions, return_type, arg_types) ⇒ Object
arg_types must be an Array.
- #declare_argument(name, type) ⇒ Object
- #declared_captures(binding = nil) ⇒ Object
- #define_class(class_def, expression) ⇒ Object
- #define_main(script) ⇒ Object
- #error(message, node) ⇒ Object
- #finish_main ⇒ Object
- #fixnum(type, value) ⇒ Object (also: #float)
- #generate ⇒ Object
- #get_binding(type) ⇒ Object
- #import(short, long) ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #log(message) ⇒ Object
- #original_create_method_builder ⇒ Object
- #scoped_body(scope, expression) ⇒ Object
- #scoped_local_name(name, scope = nil) ⇒ Object
- #toplevel_class ⇒ Object
- #with(vars) ⇒ Object
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
25 26 27 28 29 30 |
# File 'lib/mirah/jvm/compiler/base.rb', line 25 def initialize @jump_scope = [] @bindings = Hash.new {|h, type| h[type] = type.define(@file)} @captured_locals = Hash.new {|h, binding| h[binding] = {}} @self_scope = nil end |
Instance Attribute Details
#class ⇒ Object
Returns the value of attribute class.
20 21 22 |
# File 'lib/mirah/jvm/compiler/base.rb', line 20 def class @class end |
#filename ⇒ Object
Returns the value of attribute filename.
20 21 22 |
# File 'lib/mirah/jvm/compiler/base.rb', line 20 def filename @filename end |
#method ⇒ Object
Returns the value of attribute method.
20 21 22 |
# File 'lib/mirah/jvm/compiler/base.rb', line 20 def method @method end |
#static ⇒ Object
Returns the value of attribute static.
20 21 22 |
# File 'lib/mirah/jvm/compiler/base.rb', line 20 def static @static end |
Instance Method Details
#base_define_method(node, args_are_types) ⇒ Object
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 |
# File 'lib/mirah/jvm/compiler/base.rb', line 107 def base_define_method(node, args_are_types) name, signature, args = node.name, node.signature, node.arguments.args if name == "initialize" && node.static? name = "<clinit>" end if args_are_types arg_types = args.map { |arg| arg.inferred_type } if args else arg_types = args end arg_types ||= [] return_type = signature[:return] exceptions = signature[:throws] with :static => @static || node.static?, :current_scope => node.static_scope do method = create_method_builder(name, node, @static, exceptions, return_type, arg_types) annotate(method, node.annotations) yield method, arg_types end arg_types_for_opt = [] args_for_opt = [] if args args.each do |arg| if AST::OptionalArgument === arg new_args = if args_are_types arg_types_for_opt else args_for_opt end method = create_method_builder(name, node, @static, exceptions, return_type, new_args) with :method => method do log "Starting new method #{name}(#{arg_types_for_opt})" annotate(method, node.annotations) @method.start define_optarg_chain(name, arg, return_type, args_for_opt, arg_types_for_opt) @method.stop end end arg_types_for_opt << arg.inferred_type args_for_opt << arg end end end |
#begin_main ⇒ Object
98 |
# File 'lib/mirah/jvm/compiler/base.rb', line 98 def begin_main; end |
#body(body, expression) {|| ... } ⇒ Object
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 |
# File 'lib/mirah/jvm/compiler/base.rb', line 189 def body(body, expression) saved_self = @self_scope if body.kind_of?(Mirah::AST::ScopedBody) scope = body.static_scope declare_locals(scope) if scope != @self_scope if scope.self_node && scope.self_node != :self # FIXME This is a horrible hack! # Instead we should eliminate unused self's. unless scope.self_type.name == 'mirah.impl.Builtin' local_assign( scope, 'self', scope.self_type, false, scope.self_node) end end @self_scope = scope end end # all except the last element in a body of code is treated as a statement i, last = 0, body.children.size - 1 while i < last body.children[i].compile(self, false) i += 1 end yield body.children[last] if last >= 0 @self_scope = saved_self end |
#compile(ast, expression = false) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/mirah/jvm/compiler/base.rb', line 36 def compile(ast, expression = false) begin ast.compile(self, expression) rescue Exception => ex raise Mirah::InternalCompilerError.wrap(ex, ast) end log "Compilation successful!" end |
#compile_self ⇒ Object
236 237 238 239 240 241 242 |
# File 'lib/mirah/jvm/compiler/base.rb', line 236 def compile_self if @self_scope && @self_scope.self_node && @self_scope.self_node != :self local(@self_scope, 'self', @self_scope.self_type) else real_self end end |
#constructor(node, args_are_types) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/mirah/jvm/compiler/base.rb', line 160 def constructor(node, args_are_types) args = node.arguments.args || [] arg_types = if args_are_types args.map { |arg| arg.inferred_type } else args end exceptions = node.signature[:throws] method = @class.build_constructor(node.visibility, exceptions, *arg_types) annotate(method, node.annotations) with :current_scope => node.static_scope do yield(method, args) end end |
#create_method_builder(name, node, static, exceptions, return_type, arg_types) ⇒ Object
arg_types must be an Array
72 73 74 75 76 77 78 79 |
# File 'lib/mirah/plugin/gwt.rb', line 72 def create_method_builder(name, node, static, exceptions, return_type, arg_types) unless node.class == Mirah::AST::JsniMethodDefinition original_create_method_builder(name, node, static, exceptions, return_type, arg_types) else @class.build_jsni_method(name.to_s, node.visibility, static, exceptions, return_type, *arg_types) end end |
#declare_argument(name, type) ⇒ Object
185 186 187 |
# File 'lib/mirah/jvm/compiler/base.rb', line 185 def declare_argument(name, type) # declare local vars for arguments here end |
#declared_captures(binding = nil) ⇒ Object
248 249 250 |
# File 'lib/mirah/jvm/compiler/base.rb', line 248 def declared_captures(binding=nil) @captured_locals[binding || @binding] end |
#define_class(class_def, expression) ⇒ Object
175 176 177 178 179 180 181 182 183 |
# File 'lib/mirah/jvm/compiler/base.rb', line 175 def define_class(class_def, expression) with(:type => class_def.inferred_type, :class => class_def.inferred_type.define(@file), :static => false) do annotate(@class, class_def.annotations) class_def.body.compile(self, false) if class_def.body @class.stop end end |
#define_main(script) ⇒ Object
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 |
# File 'lib/mirah/jvm/compiler/base.rb', line 64 def define_main(script) @static = true @filename = File.basename(script.filename) classname = Mirah::JVM::Compiler::JVMBytecode.classname_from_filename(@filename) @type = AST.type(script, classname) @file = file_builder(@filename) # Define a main method unless all of the script's children are 'top level' # nodes (e.g. Import, ClassDefinition, InterfaceDeclaration). unless script.body.children.all? { |c| c && c.top_level? } @class = @type.define(@file) with :method => @class.main do log "Starting main method" @method.start @current_scope = script.static_scope declare_locals(@current_scope) begin_main prepare_binding(script) do script.body.compile(self, false) end finish_main @method.stop end @class.stop log "Main method complete!" else script.body.compile(self, false) end end |
#error(message, node) ⇒ Object
32 33 34 |
# File 'lib/mirah/jvm/compiler/base.rb', line 32 def error(, node) raise CompilationError.new(, node) end |
#finish_main ⇒ Object
99 |
# File 'lib/mirah/jvm/compiler/base.rb', line 99 def finish_main; end |
#fixnum(type, value) ⇒ Object Also known as: float
231 232 233 |
# File 'lib/mirah/jvm/compiler/base.rb', line 231 def fixnum(type, value) type.literal(method, value) end |
#generate ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mirah/jvm/compiler/base.rb', line 51 def generate log "Generating #{output_type}..." @file.generate do |filename, builder| log " #{builder.class_name}" if block_given? yield filename, builder else File.open(filename, 'wb') {|f| f.write(builder.generate)} end end log "...done!" end |
#get_binding(type) ⇒ Object
244 245 246 |
# File 'lib/mirah/jvm/compiler/base.rb', line 244 def get_binding(type) @bindings[type] end |
#import(short, long) ⇒ Object
228 229 |
# File 'lib/mirah/jvm/compiler/base.rb', line 228 def import(short, long) end |
#log(message) ⇒ Object
45 |
# File 'lib/mirah/jvm/compiler/base.rb', line 45 def log(); Mirah::JVM::Compiler::JVMBytecode.log(); end |
#original_create_method_builder ⇒ Object
70 |
# File 'lib/mirah/plugin/gwt.rb', line 70 alias :original_create_method_builder :create_method_builder |
#scoped_body(scope, expression) ⇒ Object
216 217 218 |
# File 'lib/mirah/jvm/compiler/base.rb', line 216 def scoped_body(scope, expression) body(scope, expression) end |
#scoped_local_name(name, scope = nil) ⇒ Object
220 221 222 223 224 225 226 |
# File 'lib/mirah/jvm/compiler/base.rb', line 220 def scoped_local_name(name, scope=nil) if scope.nil? || scope == @current_scope name else "#{name}$#{scope.object_id}" end end |
#toplevel_class ⇒ Object
47 48 49 |
# File 'lib/mirah/jvm/compiler/base.rb', line 47 def toplevel_class @class = @type.define(@file) end |
#with(vars) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/mirah/jvm/compiler/base.rb', line 252 def with(vars) orig_values = {} begin vars.each do |name, new_value| name = "@#{name}" orig_values[name] = instance_variable_get name instance_variable_set name, new_value end yield ensure orig_values.each do |name, value| instance_variable_set name, value end end end |