Class: C::Node

Inherits:
Object
  • Object
show all
Includes:
CSquare::Hashable
Defined in:
lib/csquare.rb

Instance Method Summary collapse

Methods included from CSquare::Hashable

included

Instance Method Details

#csquare_keyObject



427
428
429
# File 'lib/csquare.rb', line 427

def csquare_key
  self.to_s
end

#decorate_calls!(type_symbol, blueprint_obj) ⇒ Object



323
324
325
326
327
# File 'lib/csquare.rb', line 323

def decorate_calls! type_symbol, blueprint_obj
  each do |n|
    n.decorate_calls! type_symbol, blueprint_obj
  end
end

#expand_node(node) ⇒ Object

Takes some assignment expression node and turns it into a regular assign.

e.g., x = y —> x = x y



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/csquare.rb', line 399

def expand_node node
  return node unless node.AssignmentExpression? && !node.Assign?

  # Figure out the new class
  begin
    klass    = C.const_get(node.class.to_s[3...-6])
    new_node = C::Assign.new(:lval => node.lval.clone, :rval => klass.send(:new, :expr1 => node.lval, :expr2 => node.rval))

    node.replace_with(new_node)
    new_node
  rescue NameError => e
    STDERR.puts "Invalid CAST class #{node.class.to_s[3...-6].inspect}"
    raise e
  end
end

#has_local_below?(var) ⇒ Boolean

Is some local ‘var’ declared anywhere in this scope or below? (Is it unsafe to declare it in this scope?)

Returns:

  • (Boolean)


417
418
419
420
421
422
423
424
425
# File 'lib/csquare.rb', line 417

def has_local_below? var
  return true if self.respond_to?(:locals) && locals.has_key?(var)

  self.each do |n|
    return true if has_local_below?(var)
  end

  false
end

#new_temp_local(type) ⇒ Object



330
331
332
# File 'lib/csquare.rb', line 330

def new_temp_local type
  parent_block.new_temp_local(type)
end

#new_temp_statement(statement, before_statement) ⇒ Object



334
335
336
# File 'lib/csquare.rb', line 334

def new_temp_statement statement, before_statement
  parent_block.new_temp_statement(statement, before_statement)
end

#opObject



305
306
307
# File 'lib/csquare.rb', line 305

def op
  CSquare::Generator::CAST_TO_OP[self.class]
end

#parent_blockObject

Find the parent block



340
341
342
343
344
345
346
# File 'lib/csquare.rb', line 340

def parent_block
  p = self.parent
  while !p.Block?
    p = p.parent
  end
  p
end

#parent_function_defObject

Find the parent FunctionDef



359
360
361
362
363
364
365
# File 'lib/csquare.rb', line 359

def parent_function_def
  p = self.parent
  while !p.FunctionDef?
    p = p.parent
  end
  p
end

#parent_stmtObject



349
350
351
352
353
354
355
# File 'lib/csquare.rb', line 349

def parent_stmt
  p = self.parent
  while !p.Statement?
    p = p.parent
  end
  p
end

#recombine!(function, blueprint, type_symbol, return_type = nil) ⇒ Object

Replace some expression with the pattern from Blueprint.



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/csquare.rb', line 369

def recombine! function, blueprint, type_symbol, return_type=nil

  args_types = {}

  if respond_to?(:recombine_recursive!)
    self.recombine_recursive! function, blueprint, type_symbol, return_type
  else

    self.fields.each_with_index do |f,i|
      n = self.send(f.reader)

      if n.respond_to?(:recombine!)
        n = expand_node(n) if blueprint.has_op?(n.op)
        result = n.recombine! function, blueprint, type_symbol, return_type
        replace_node(n, result[0]) unless result.nil? || result[0].nil? || result[0] == n
      end
    end
  end

  if respond_to?(:return_typename)
    [self, self.return_typename(function, blueprint)]
  else
    [self, nil]
  end
end

#recursively_replace_casts!(typenames_and_types) ⇒ Object

Recursively replace typenames with actual types.



310
311
312
313
314
# File 'lib/csquare.rb', line 310

def recursively_replace_casts! typenames_and_types
  each do |n|
    n.recursively_replace_casts! typenames_and_types
  end
end

#replace_types!(typenames_and_types) ⇒ Object



316
317
318
319
320
# File 'lib/csquare.rb', line 316

def replace_types! typenames_and_types
  each do |n|
    n.replace_types! typenames_and_types
  end
end